Constructor is a special method that will run automatically when an object is created, ie when the "new" command is executed. Constructors are commonly used to create initial processes in preparing objects, such as assigning initial values to properties, calling internal methods and some other processes used to 'prepare' objects. The constructor is created using the method __construct().
Destructor is a special method that runs automatically when an object is deleted. In PHP, the entire object is automatically deleted when the PHP page where the object is located is finished processing. But we can also delete objects manually. Destructor is usually used to 'clean' some variables, or run certain processes before an object is deleted. The destructor is created using method __destruct().
Constructor and Destructor are part of OOP (Object-Oriented Programming) learning.
PHP has a feature of 'overall removal of the object' known as Garbage Collection. The garbage collection feature will delete all objects automatically when the PHP page is finished in execution, so it will call the __destructor method of all objects. But PHP provides a way if we want to delete the object before the mechanism is running, that is, use the unset() function or fill the object variable with a null value.
Here is an example of using the constructor and destructor in PHP programming below :
<?php
// make class tv
class tv {
private $owner = "simpleawesome";
private $brand = "Samsung";
public function __construct(){
echo "It comes from the Constructor the TV";
}
public function turnon_tv(){
return "Turn on the $this->brand TV have $this->owner";
}
public function __destruct(){
echo "It comes from Destructor the TV";
}
}
// create object from class TV (instatiation)
$tv_simpleawesome= new tv();
echo "
";
echo $tv_simpleawesome->turnon_tv();
echo "
";
?>
and type the program code above in the text editor.
In the example above, we create a class tv with 3 methods :
__construct() method is the constructor of the class tv. This method will be called automatically when the class tv is instantiated.turnon_tv() method is a 'usual' method that will output string results. To use this method, we call it from the object.__destruct() method is a destructor of the class tv. This method will be called when the object is deleted.After defining the class, we create the $tv_simpleawsome object, and call the method turnon_tv(). Here are the results :
Based on the program code above, the __construct() and __destruct() methods are automatically invoked when an object is created and when an object is deleted.
To try removing the $tv_simpleawesome object manually, we can use the unset() function as below :
// delete $tv_simpleawesome object
unset($tv_simpleawesome);
echo "
";
echo "The object has been destroyed";
and add the function under object $tv_simpleawesome = new tv() (or advanced program we have previously created). Then the program code will be like this :
<?php
// make class tv
class tv {
private $owner = "simpleawesome";
private $brand = "Samsung";
public function __construct(){
echo "It comes from the Constructor the TV";
}
public function turnon_tv(){
return "Turn on the $this->brand TV have $this->owner";
}
public function __destruct(){
echo "It comes from Destructor the TV";
}
}
// create object from class TV (instantiation)
$tv_simpleawesome= new tv();
echo "
";
echo $tv_simpleawesome->turnon_tv();
echo "
";
// delete $tv_simpleawesome object
unset($tv_simpleawesome);
echo "
";
echo "The object has been destroyed";
?>
Type the program code above in the text editor. It is advisable to create a new file to distinguish programs that use the unset() function.
Here are the results :
After calling $tv_simpleawesome-> turnon_tv(), then we delete the $tv_simpleawesome object manually using the unset($tv_simpleawesome) function.
To prove that the $tv_simpleawesome destructor is running, we added the echo "The object has been destroyed" command at the end of the page. So we can see that the $tv_simpleawesome destructor object is run before it is automatically deleted by PHP. Please try remove the unset($tv_simpleawesome) command, then the string "The object has been destroyed" will appear before the $tv_simpleawesome destructor object.
- We can also replace the
unset($tv_simpleawesome)command with$tv_simpleawesome = null. These two commands will remove the$tv_simpleawesomeobject and trigger the destructor call.