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.

Đang xem: Object oriented php tutorial for beginners: steps 12 to 17

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.

Xem thêm:

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.

class User{ //Declare class variablespublic $name;public $email;public $phone;//Define defualt constructorfunction __construct() { echo “

It is a default constructor.

“; $this->name = “Meher Nigar”;$this->email = ““;$this->phone = “8801767354290”;} } //Create object$objuser = new User(); //Print the values of class variables secho “

Name: “.$objuser->name.”

“;echo “”;echo “

Phone: “.$objuser->phone.”

“;?>

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.

Xem thêm:

*

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.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *