Constructor of Classes and Objects in OOP PHP

A constructor is a special functions which are automatically called when an object is created . The constructor is the most useful , especially because it allows you to send parameters along when creating a new object, which can then be used to initialize variables on the object. Constructor is nothing but a function defined in your PHP class. Constructor function automatically called when you will create object of the class. As soon as you will write $object = new yourClass() your constructor function of the class will be executed. you can create constructor by defining magic function __construct.

Example:

class MyClass 
{
        public $var;

    // Class constructor
    public function __construct($var)
    {
        echo 'Created an object of MyClass';
        $this->var = $var;
    }

}
// Make an object
$objA = new MyClass('A');

// Call an object method to show the object’s property

If you have created parameter in the constructor you need to pass value for them on the time of object creation.
$objCls = new MyClass(‘A’). If you will not send value PHP will throw error.

Read more on OOP in PHP