Ending Chapters in Code with Destructive Grace
Destructors are the opposite of constructors…go figure. The destructor method is called after the object is not referenced any more. In PHP, the constructor is called first, followed by any internal methods that the user accesses, followed by the destructor method for final cleanup.
The destructor is even called if the PHP script is exited using PHP’s exit() function.
https://www.dinocajic.com/php-constructors/
To create a destructor method, we’ll follow a similar naming convention to the constructor method. You start with two underscores, followed by the keyword destruct: __destruct().
<?php
class ClassName {
//...
public function __destruct() {
//...
}
}
Even though you may be tempted to clean-up resources in the destruct method, it’s usually good practice to clean them up as soon as you’re done using them.
Let’s take a look at our favorite examples: the GermanShepherd and the Car classes.
The destructor method is usually placed as the last method in a class so that someone reading your code has an easier time following it. A quick recap: we normally have our constants listed first, followed by properties, the construct method, all other methods, and then finally, the destruct method.
What would be a good example to list out in our destructor? In our GermanShepherd class, the properties are going to be modified throughout the life of the object. If we wanted to see what those properties looked like at the very end, right before the object is destroyed, we could utilize the destructor. Our destruct method will just var_dump the properties of the object.
<?php
// Destructor
class GermanShepherd {
//...
public $eye_color;
public $dob;
public $does_shed = true;
public $kingdom = "Animalia";
public $phylum = "Chordata";
public $class = "Mammalia";
public $order = "Carnivara";
public $family = "Canidae";
public $genus = "Canis";
public $species = "Canis Lupus";
public $subspecies = "Canis Lupus Familiaris";
public $breed = "German Shepherd Dog";
public $fur_color;
public function __construct( string $eye_color,
string $dob,
string $fur_color )
{
$this->eye_color = $eye_color;
$this->dob = $dob;
$this->fur_color = $fur_color;
}
//...
public function bark() {
echo "I'm barking";
}
//...
public function __destruct()
{
var_dump(
[
"eye_color" => $this->eye_color,
"dob" => $this->dob,
"does_shed" => $this->does_shed,
"kingdom" => $this->kingdom,
"phylum" => $this->phylum,
"class" => $this->class,
"order" => $this->order,
"family" => $this->family,
"genus" => $this->genus,
"species" => $this->species,
"subspecies" => $this->subspecies,
"breed" => $this->breed,
"fur_color" => $this->fur_color,
]
);
}
}
$gs_dog = new GermanShepherd( "Brown", "Jan 02, 2012", "Black" );
$gs_dog->bark();
In the example above, we’re creating a new GermanShepherd object and calling the bark() method. Since we now have a __destruct() method, the output is going to be interesting.
As is evident, the destruct method was called as soon as the execution was complete. We called the bark() method first. PHP executed the script and said, “there are no additional methods that I have to execute. Does a __destruct() method exist? Yes. Let me execute it as well.” We explicitly called the bark() method and PHP implicitly called the __destruct() method.
For the Car class, I’ll show you something that I see quite frequently amongst developers. Programmers tend to close their connection to the database in the __destruct() method. You’re going to want to terminate your database connection as soon as you’re done using it, but this will work especially if you inject or initialize the database connection in your constructor.
We are going to cover databases in full later, so now we’ll just echo out a statement that says, “Connection Successfully Closed.” We can even add the “Connection Successfully Established” in our constructor to see the full scope of execution at work.
<?php
// Car Class
class Car {
//...
public function __construct( int $year, string $make, string $model, // ... )
{
// ...
echo "Connection Successfully Established";
}
public function get_make_and_model(): string
{
return $this->make . " " . $this->model;
}
//...
public function __destruct()
{
echo "Connection Successfully Closed";
}
}
$mitsubishi = new Car(2014, "Mitsubishi", "EVO");
echo $mitsubishi->get_make_and_model();
The code outputs the “Connection Successfully Established” string, followed by the Make and Model of the vehicle and then finally displays the “Connection Successfully Closed” string as was anticipated. PHP called the constructor, called the method, and finally called the destructor in that order.
Continue your learning with these articles
ERECTING WORLDS FROM BLUEPRINTS WITH CONSTRUCTORS
If you’re used to other object oriented programming languages, you’re probably familiar with the constructor. You may also be familiar with the constructor being named as the class name; it’s just a method that shares the same name as the class name.
Ending Chapters in Code with Destructive Grace
Destructors are the opposite of constructors…go figure. The destructor method is called after the object is not referenced any more
NAVIGATING LOGIC’S SHADOWS WITH VISIBILITY KEYS
PHP – P51: VISIBILITY MODIFIERS
All about visibility modifiers. We tackled public, private, and protected visibility modifiers previously, but we get to understand what they are now.