What is the Constructor Method in PHP?

PHP OOP Constructor
The method that automatically executes the first time when creating a new object from class, it’s called constructor method in PHP. Constructor Method is predefined Public Method. To declare any constructor, we have to write two times the underscore (_) before the construct word after the public function. That’s mean we have to declare any constructor like this: public function __construct(). Let’s see an Example:
<?php class oop{ public function __construct(){ echo "Welcome to PHP World!"; } public function sayHello(){ echo "Hello World"; } } $obj=new oop; /* Now Automatically Execute __construct Method And Will show "Welcome to PHP World " First.*/ echo "<br>"; $obj->sayHello(); // Result: Hello World ?>
Result
Welcome to PHP World Hello World
How to Send Argument to the Parameter of Constructor Method?
If there is a Parameter in a Constructor Method of a Class, Then if you want to send any arguments to that constructor Method, the arguments must be sent with parenthesis to the class object. See the example below:
class Car { private $model; //__construct public function __construct ($model) { $this -> model = $model; } public function getCarModel() { return ' The car model is: ' . $this -> model; } } //We pass the value of the variable once we create the object $car1 = new Car("Mercedes"); // Passing Argument to Constructor Parameter echo $car1 -> getCarModel();
Result:
The car model is: Mercedes.
What is the Destructor Method in PHP?
When the object has finished its job, then the method in the class which is automatically executed or execute at the end, that method is called Destructor Method in PHP. Destructor Method is predefined Public Method. To declare any destructor, we have to write two times the underscore (_) before the destruct word after the public function. That’s mean we have to declare any destructor like this: public function __destruct(). Let’s see an Example:
<?php class oop{ public function __construct(){ echo "Welcome to PHP World!"; } public function sayHello(){ echo "Hello World"; } public function __destruct(){ echo "<br>I'm about to disappear - bye bye!"; } } $obj=new oop; echo "<br>"; $obj->sayHello(); ?>
Result:
Welcome to PHP World! Hello World I'm about to disappear - bye bye!
Can the Destructor Method in PHP receive an argument?
No it can’t. Actually it doesn’t need to. However, you can receive arguments with any other method and can put that argument to any property and you can return that property via Constructor Method. See the example below:
<?php class oop{ private $name; public function __construct($val){ return $this->name=$val; } public function sayHello(){ echo "Hello World"; } public function __destruct(){ echo "<br>Bye bye $this->name!"; } } $obj=new oop("Imran"); echo "<br>"; $obj->sayHello(); ?>
Result
Hello World Bye Bye Imran!
Why Will You Use Destructor Method?
Destructor Method is used to finish anything. Works like file closing, database connection, and session destruction are done by Destructor Method.

Hi, My name is Masud Alam, love to work with Open Source Technologies, living in Dhaka, Bangladesh. I’m a Certified Engineer on ZEND PHP 5.3, I served my first Fifteen years a number of leadership positions at AmarBebsha Ltd as a CTO, Winux Soft Ltd, SSL Wireless Ltd, Canadian International Development Agency (CIDA), World Vision, Care Bangladesh, Helen Keller, US AID and MAX Group where I worked on ERP software and web development., but now I’m a founder and CEO of TechBeeo Software Company Ltd. I’m also a Course Instructor of ZCPE PHP 7 Certification and professional web development course at w3programmers Training Institute – a leading Training Institute in the country.