php

Use of Constructor in PHP

The constructor is an essential part of object-oriented programming. It is a method of a class that is called automatically when an object of that class is declared. The main purpose of this method is to initialize the object. Without object initialization, the constructor method can also be used to call the parent constructor and any private or public method that is required at the time of object creation. PHP supports the constructor method like other programming languages.

How different types of constructors can be defined and used in the PHP class are shown in this tutorial.

Advantages of using constructor:

First, it can be used to call any methods of the class with the initialization of the class variables. Second, it can be used to re-use the object multiple times without re-initializing it after creating the object. Third, the child constructor can call the parent constructor if requires. Lastly, the common tasks that are required to be done one time can be done easily by using a constructor, such as session creation.

Types of Constructors:

Mainly three types of constructors are used in any object-oriented programming. These are mentioned below:

Default Constructor

This constructor does not contain any argument, and it is declared by the name, __construct(). The default values can be assigned to the class members, and the other methods of the class can be called dynamically by using the default constructor.

Parameter-less Constructor

If any method in the class is declared with the class name and does not contain any argument, then that method is called a parameter-less constructor. It works like the default constructor. It is also called a user-defined constructor.

Parameterized Constructor

The user-defined constructor that contains an argument is called a parameterized constructor. The argument values of this constructor are passed at the time of the object creation and the other methods of the class can also be called by this constructor.

The different uses of the constructors in object-oriented PHP script are shown in the next part of this tutorial.

Example-1: Use of default constructors

The following script shows the use of the default constructor in PHP. Here, the User class contains three class variables and the default constructor method that will initialize the class variables with the default values at the time of object creation. The values of the class variables will be printed later using the object of the class.

<?php
class User
{
//Declare class variables
public $name;
public $email;
public $phone;
//Define defualt constructor
function __construct()
{
echo "<h3>It is a default constructor.</h3>";
$this->name = "Meher Nigar";
$this->email = "nigar@gmail.com";
$this->phone = "8801767354290";

}
}

//Create object
$objuser = new User();
//Print the values of class variables s
echo "<p>Name: <b>".$objuser->name."</b></p>";
echo "<p>Email: <b>".$objuser->email."</b></p>";
echo "<p>Phone: <b>".$objuser->phone."</b></p>";
?>

Output:

The following output will appear after running the script. When the object of the class, $objuser, is declared, then the default constructor method, __construct(), is called automatically and initialized the class variables with default values.

Example-2: Use of user-defined parameter-less constructors

The following script shows the use of the user-defined parameter-less constructor using a PHP script. Here, the constructor method is declared with the name of the class to initialize the class variables with the default values, and a display() method is declared to print the values of the class variables.

<?php
class User
{
//Declare class variables
public $name;
public $email;
public $phone;
//Define user-defined parameter-less constructor
function User()
{
echo "<h3>It is an user-defined constructor.</h3>";
$this->name = "Mostafijur Rahman";
$this->email = "rahman@gmail.com";
$this->phone = "880185453452";
$this->display();

}

function display()
{
//Print the values of class variables s
echo "<p>Name: <font color="blue">".$this->name."</font></p>";
echo "<p>Email: <font color="blue">".$this->email."</font></p>";
echo "<p>Phone: <font color="blue">".$this->phone."</font></p>";

}
}

//Create object
$objuser = new User();

?>

Output:

The following output will appear after running the script. When the object of the class, $objuser, is declared, then the parameter-less constructor method, User(), is called automatically and initialized the class variables with the default values.

Example-3: Use of user-defined parameterized constructor

The following script shows the use of a user-defined parameterized constructor using a PHP script. The constructor named, User(), contains three parameters here that will be used to store the values passed at the time of object creation and initialize the class variables with those values. The display() method will be used to print the class variables.

<?php
class User
{
//Declare class variables
public $name;
public $email;
public $phone;
//Define user-defined parameter-less constructor
function User($name, $email, $phone)
{
echo "<h3>It is an user-defined constructor.</h3>";
$this->name = $name;
$this->email = $email;
$this->phone = $phone;

}

function display()
{
//Print the values of class variables
echo "<p>Name: <font color="green">".$this->name."</font></p>";
echo "<p>Email: <font color="green">".$this->email."</font></p>";
echo "<p>Phone: <font color="green">".$this->phone."</font></p>";

}
}

//Create object
$objuser = new User('Mir Sabbir','sabbir@gmail.com','01645627748');
//Call display function
echo $objuser->display();

?>

Output:

The following output will appear after running the script. Here, the display() method is called by using the class object.

Example-4: Calling parent constructor from child constructors

The following script shows how the constructor of the parent class can be called inside the constructor of the child class. Here, the c1 is the parent class that has a default constructor and a class variable named $a. The c2 is the child class that has a default constructor, where the parent constructor has been called by using the parent keyword and a class variable named $b. The class, c2, has the method, add(), to calculate the sum of $a and $b.

<?php

//Parent class
class c1{
//Define class variable
public $a = 10;
//Parent constructor
function __construct(){
echo "<h1>It is a default constructor of parent class.</h1>";
}
}

//Child class
class c2 extends c1{
//Define class variable
public $b = 40;
//Child constructor
function __construct(){
parent::__construct();
echo "<h2>It is a default constructor of child class.</h2>";
}

//Define function for addition
function add()
{
$result = $this->a + $this->b;
echo "<h3>The sum of ".$this->a." and ".$this->b." is ".$result."</h3>";
}
}

//Define object
$object=new c2();
//Call method of child class
$object->add();
?>

Output:

The following output will appear after running the script. The parent constructor has been called here to initialize the value of the variable, $a.

Video Tutorial

Conclusion:

The uses of different types of constructors in object-oriented PHP has been shown in this tutorial by using simple examples to help the readers know the features of the constructor and apply it properly in their script.

About the author

Fahmida Yesmin

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.