OOPs PHP Questions & Answer
- Interfaces are defined using the interface keyword.
- All methods declared in an interface must be public. Classes defined as Interface may not be instantiated(create object).
- To extend the interface class, implements operator is used.
- You can inherit number of interface class at the time of extending and number of abstract class separated by comma.
- All methods in the interface must be implemented within a child class; failure to do so will result in a fatal error.
- Interfaces can be extended like classes using the extends operator.
- The class implementing the interface must use the exact same method signatures as are defined in the interface. Not doing so will result in a fatal error.
- Type hint & number of parameter must be match.
- Traits are a mechanism for code reuse in single inheritance.
- A Trait is similar to a class, but only intended to group functionality in a fine-grained and consistent way.
- It is not possible to instantiate a Trait but addition to traditional inheritance. It is intended to reduce some limitations of single inheritance to reuse sets of methods freely in several independent classes living in different class hierarchies.
- Multiple Traits can be inserted into a class by listing them in the use statement, separated by commas(,).
- If two Traits insert a method with the same name, a fatal error is produced.
PHP introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final.
If the class itself is being defined final then it cannot be extended. If the function itself is being defined final then it cannot be extended.
$this is used to reference the current object, whereas self is used to access the current class itself.
In other words, use $this->member for non-static members, use self::$member for static members.
There are two distinct ways to access methods in PHP. The double colon operator (::) is for static elements. This means an instance of the object has not been created yet. while The arrow operator(->) is for instance elements.
A constructor is a special kind of a method. It is automatically called when the object is created. The purpose of the constructor is to initiate the state of the object. The name of the constructor in PHP is __construct() (with double underscores).
There are 3 type of constructor in php -
- Default - __construct() is not declared, then uses default. By default it is non-Parameterized.
- Parameterized - __construct() is declared with parameter:
- Copy - __clone(). it is used to create copy one.
Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required. If the child does not define a constructor then it may be inherited from the parent class just like a normal class method (if it was not declared as private).
Destructor method will be called as soon as there are no other references to a particular object, or in any order during the shutdown sequence.
Function which is automatically called in case you are trying to use a class/interface which hasn't been defined yet. By calling this function the scripting engine is given a last chance to load the class before PHP fails with an error