PHP Constructor

PHP Constructor


Construct:
The Constructor method is a special type of function called __construct within the class body.A constructor allows you to initialize an object's properties upon creation of the object.
To declare /create a constructor method, use the __construct name (begins with two underscore "__").

This method is always "public" even if this attribute is not specified.
The difference from the other functions is that a constructor method is automatically invoked when an object is created.
that using a constructor saves us from calling the method which reduces the amount of code:


creating construct function :

Output : Your name : Alex
               Your age : 30

construct method has Two  parameter ($name,$age) [you can add more parameters or none, as any function], when it creates an object instance of the class you must add and an argument too.