Sunday, June 11, 2017

Interview Questions and Answers - Object Orient Programming in PHP (OOP) - Intermediate

Question. No. 1:
What are magic methods ?

Answer:
  • __construct : called while initializing an object with a "new" keyword
  • __destruct : when object is going out of scope.
  • __call : When method doesn't exists or access restricted in Object scope
  • __callStatic : When method doesn't exists or access restricted in Static Scope
  • __get : when trying to read a variable which doesn't exist or access restricted
  • __set: when trying to set a variable which doesn't exist or access restricted
  • __isset : when trying to check isset a variable which doesn't exist or access restricted
  • __unset : when trying to unset a variable which doesn't exist or access restricted
  • __sleep :  when serialize an object in order to store in database 
class myClass
{

       var $var1 = 3; 

private $var2 = 2;
    public function __sleep()
    {
        return array('var1', 'var2');
    }
}
$myObj= new myClass;

serialize($myObj);
  • __wakeup : when un-serialize an object get serialize object then un-serialize it 
class myClass
{

       var $var1 = 3; 

private $var2 = 2;
    public function __wakeup ()
    {
        return array('var1', 'var2');
    }
}
$myObj= new myClass;

$se = serialize($myObj);

unserialize($se);
  • __toString: when a class object was printed as string this method will be called
class myClass
{
    public function __toString()
    {
        return 'class name';
    }
}

$myObj = new myClass;
echo $myObj;
  • __invoke : when declared ,it will be used as a callable function with instantiated object name. it takes same number of arguments passed.
class myClass
{
    public function __invoke($x,$y)
    {
        echo $x.$y;
    }
}
$myObj = new myClass;
$myObj (4,5);

  • __set_state : when var_export() function called on the object
  • __clone : when called clone on an object. when we try to tract the serials of cloned.
  • __autoload : its not a method but Function, 






Wednesday, June 7, 2017

Interview Questions and Answers - Object Orient Programming in PHP (OOP) - Basics

Question.1. 
what is class?

Answer: 

  • Its a blue print (Technical Design/structure) of how methods & behaviors will be written/shown. 

Question.2:
What is an object?

Answer:

  • Its an instance of a class.


Question.3.
What is the Interface?
Answer:

  • functionality cannot be provided
  • properties cannot be defined
  • it is to enforce child class to implement all the methods existing in interface is a MUST. 
  • it cannot be instantiated. 
  • methods can only be public inside interface.
  • child class can only implement not extend with interface
  • every method should be abstracted 
  • multiple inheritance is possible with implements keyword
  • multi-level inheritance is possible , that is an interface can extend parent interface and a class can implement child interface and it should implement all the methods from parents interfaces.

Question 4:
What is an Abstract class?

Answer:

  • functionality can be provided inside it
  • properties can be defined inside it
  • Methods cannot be private, 
  • child class must implement abstract methods
  • child class can extend an abstract class with "extends" keyword instead of "implements" 
  • not every method should be abstract
  • multiple inheritance is not possible

Question 5:
What are properties?

Answer:

  • information / data/state of an object
Question 6:
What is are Methods?

Answer:

  • Functions or behaviors of the class
Question 7:
What is Encapsulation?

Answer:

  • to enclose the properties and behaviors in curly braces so that we don't need to know the information processing inside a class while working with an API calls to this class.
Question 8:
What are Visibility/Access Modifiers?
Answer:

  • Public - accessible from everywhere
  • Protected - accessible from within class as well child classes
  • private  - accessible from within class ONLY 
Question 9:
What is Static?

Answer:
  • Method or property available at class level , no object needed
  • changes will affect everywhere in all the instances of the class
Question 10:
What is Constructor?

Answer:
  • Its a magic method
  • used to initialize the properties of an object 
Question: 11:
What is Polymorphism:

Answer:
Its a concept that an object can have different forms, means a class can have many child classes which are related to parent class but having different behaviors.

example:

interface Speaking{
     public function speak();
}

cat implements speaking{
     
     public function speak(){
           echo 'meeaoo';
     }
}

dog implements speaking{
     
     public function speak(){
           echo 'woooah';
     }
}

person implements speaking{
     
     public function speak(){
           echo 'lets go';
     }
}