Constructor in PHP. The constructor is the PHP Object Oriented Programming idea. The constructor is combined with the classes we state in the program. The constructor is called automatically when the object of the class is initiated, so the definition of constructor performs like this, ‘A constructor is a specific way which is ordered automatically when the object of the class is initiated’. In this topic, we are going to study constructors in PHP.
<?php Class Classname { //constructor body function __constructor() { //statements start }} ?>
In the above part of the code, the constructor starts with an __ double underscore supported by the keyword construct. A constructor without __construct() or in case the name of the class is modified then the method described is just a method and not a constructor. So, it is necessary to determine the constructor in a class as per the syntax specified.
Types of Constructors in PHP
Pre-defined Constructor
To clarify about Pre-defined Constructor, Let’s see at an example mentioned below
Example of Pre-defined Constructor
<?php class Man { function Speak() { echo " This is Not a constructor method " . '<br>'; } function __construct() { echo "This method called constructor method " . '<br>'; } } $p = new Man(); $p->Speak(); ?>
In the above example, we have a class Man, which has two methods, where a person Speak() Method and the constructor method __constructor(). Next, we have initiated the class as an object $i. Using the $i we have called the normal method. As quickly as the object is created the constructor method is called and the statement inside the method is executed, similarly further the Speak() method which is a normal method and not a constructor method is called using the same object $i and the statement inside that method is executed. Also, since the constructor described above does not have any arguments we will call it a zero-argument constructor or predefined constructor.
How does Constructor in PHP Work?
Let us take a glance at the base class constructor and derived class which increases the base class using the extends keyword which has its own constructor to run and the parent constructor to run as well. Till now we read only about the constructor published in a class. Let us continue some more knowledge to the constructor here. With the mentioned example, the base class MAn has a constructor, now this constructor is called by the derived class or the Sub Class by using the parent keyword and so can reach the Base Class Man constructor.
The Output flow of Constructor in PHP
First, the Man constructor is called, then the Customer constructor is called which inside calls again the Man constructor and then its individual customer constructor, and last the Employee class which increases the Man class thus the Person constructor is called again.
<?php class Man { function __construct() { echo "In Man constructor"."<br>"; } } class Customer extends Man { function __construct() { parent::__construct(); echo "In Customer constructor"."<br>"; } } class Employee extends Man { // inherits Man's constructor } // In Man constructor $p = new Man(); // In Man constructor // In Customer constructor $c = new Customer(); // In Employee constructor $e = new Employee(); ?>
In the above example, we have determined how the set method and the get method works. Using the encapsulation concept in OOP. Originally, the program is published with a constructor, a set_name method, and a get_name method. Note that the constructor is a parameterized constructor which is surely called when the class is instantiated and therefore the first output is Muhammad Rauf next formed an object of the class and called the set_name and get_name methods which print the output as Haleema.
The Access Specifier
There are three access specifiers in PHP
- Public
- Protected
- Private
Public: Members of the class declared as public are available everywhere.
Protected: Members of the class represented as protected are available only within the base class and the derived class which extends the base class.
Private: Members of the class represented as private are available with the class that defines it.
Also, the variables represented are called data members or characteristics, and the functions represented are called data methods. In the below example we have Base Class represented as Person which has the following properties along with the access specifiers public name, protected email, and private mobile. Now the class is initiated with an object $i and these three properties are obtained from the object. which outputs are an error, why because, the protected property says that protected are available only within the base class and the derived class which extends the base class?
Conclusion Constructor in PHP
Hope this article gains you with what you have been searching for. The article has various examples for you to learn. The deeper you put the examples into practice the easier it will enhance understanding.