What is the use of constructor?

Constructor are automatically invoked when the object is created, so it’s used for initialization of member variables. And the second use is to execute some code in object initialization time, if we want some code to be executed automatically whenever the class object is created. The code that we want to execute must be put in the constructor.

What is the meaning of a final class and a final method?

‘final’ is introduced in PHP5. Final class means that this class cannot be extended and a final method cannot be overrided.

By declaring a class or function final, it cannot be overridden. A class declared final will have no child classes, and function in a class declared final cannot be overridden in a child class.

The final keyword on functions is useful when the function is critical to the consistent state of the object. If you have a delete function you might wish to declare it final, so classes that extend it cannot prevent deletion.

A class is useful to be declared final when you do not want its implementation to be changed, the primary example is the String class  which is immutable.

Differences between abstract class and interface in PHP ?

Following are some main difference between abstract classes and interface in php

  1. In abstract classes this is not necessary that every method should be abstract. But in interface every method is abstract.
  2. Multiple and multilevel both type of inheritance is possible in interface. But single and multilevel inheritance is possible in abstract classes.
  3. Method of php interface must be public only. Method in abstract class in php could be public or protected both.
  4. In abstract class you can define as well as declare methods. But in interface you can only defined your methods.

What are Abstract Classes ?

Abstract classes are those classes which can not be directly initialized. Or in other word we can say that you can not create object of abstract classes. Abstract classes always created for inheritance purpose. You can only inherit abstract class in your child class.:

When inheriting from an abstract class, all methods marked abstract in the parent’s class declaration must be defined by the child; additionally, these methods must be defined with the same visibillity.



abstract class MyAbstractClass {
abstract function myAbstractFunction() {


  • In abstract class at least one method must be abstract.
  • we can not create object of abstract class.
  • Abstract classes may not be instantiated.
  • The child classes which inherits the property of abstract base class, must define all the methods declared as abstract.
  • Any class that contains at least one abstract method must also be abstract.
  • If the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private.
  • Abstract class can contain variables and concrete methods.

Note that function definitions inside an abstract class must also be preceded by the keyword abstract. It is not legal to have abstract function definitions inside a non-abstract class.



What is the alternative to the multiple inheritance in PHP

Interfaces : 

Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled.

Interfaces are defined using the interface keyword, in the same way as a standard class, but without any of the methods having their contents defined.

All methods declared in an interface must be public; this is the nature of an interface.

Traits  :

As of PHP 5.4.0, PHP implements a method of code reuse called Traits.

Traits are a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.