PHP, like many other object-oriented programming languages, supports class constructors and destructors.
A constructor is a specific function that is automatically invoked when a class object is formed. A constructor’s principal function is to initialize the object’s properties and set them to default values or values supplied as arguments when the object is formed. Constructors are declared in PHP with the double underscore (__) syntax and the phrase “construct”. “__construct” must be the name of the constructor method. Here’s an example of a constructor in action in the “Pet” class:
class Pet {
public $nick_name;
public $color;
function __construct($name, $color) {
$this->nick_name = $name;
$this->color = $color;
}
function get_name() {
return $this->nick_name;
}
function get_color() {
return $this->color;
}
}
$cat = new Pet("Whiskers", "Black");
echo $cat->get_name();
echo "<br>";
echo $cat->get_color();
When we create a new “Pet” object and put in “Whiskers” and “Black” as parameters, the constructor sets the object’s “nick name” and “color” properties to “Whiskers” and “Black,” respectively. The values of these attributes may then be retrieved using the “get name” and “get color” methods.
A destructor, on the other hand, is a particular procedure that is invoked whenever an object is destroyed or removed from memory. Its primary function is to liberate resources that the object was utilizing, such as terminating database connections or clearing memory. Destructors are declared in PHP with the double underscore (__) syntax and the word “destroy”. “__destruct” must be the name of the destructor method. Here’s an example of a class destructor in action:
class Pet {
public $nick_name;
public $color;
function __construct($name, $color) {
$this->nick_name = $name;
$this->color = $color;
}
function get_name() {
return $this->nick_name;
}
function get_color() {
return $this->color;
}
function __destruct() {
echo "Goodbye, " . $this->nick_name . "!<br>";
}
}
$cat = new Pet("Whiskers", "Black");
echo $cat->get_name();
echo "<br>";
echo $cat->get_color();
In this example, when the script terminates or the “Pet” object is destroyed, the destructor is immediately invoked and displays “Goodbye, Whiskers!” on the screen.
It is vital to understand that in PHP, constructors and destructors are not performed manually; they are called automatically when an object is created or destroyed. You may also specify default values for constructor arguments. For example, if you want the constructor’s second parameter to be optional, you may define a default value like this:
function __construct($name,$color = "Black") {
$this->nick_name = $name;
$this->color = $color;
}
In this example, if no value for the second parameter is provided when the object is created, the “color” property will automatically be set to “Black”.
It’s also important to note that if a child class extends a parent class and wants to call the parent class constructor in its own constructor, it can use the syntax “parent::__construct()” to do so. If the child class does not have its own constructor, the constructor of the parent class will be inherited as a normal function, unless it is declared as private.
In summary, constructors and destructors are special methods that are used to initialize and release resources for objects in PHP. They are automatically called when an object is created or destroyed and can be used to set default values for object properties or to perform other tasks.
Q&A
Q: What is a constructor in PHP?
A: A constructor is a specific function that is automatically invoked when a class object is formed. Its primary function is to initialize the object’s properties, setting them to default values or values supplied as arguments when the object is created. Constructors are specified using the double underscore (__) syntax together with the word “construct”, and the name of the constructor method must be “__construct”.
Q: How is a constructor declared in PHP?
A: Constructors are declared in PHP with the double underscore (__) syntax and the phrase “construct”. “__construct” must be the name of the constructor method.
Q: What is the purpose of a destructor in PHP?
A: When an object is destroyed or deleted from memory, a destructor method is automatically invoked. Its primary function is to liberate resources that the object was utilizing, such as terminating database connections or clearing memory.
Q: How is a destructor declared in PHP?
A: In PHP, destructors are declared using the double underscore (__) syntax along with the word “destruct”. The name of the destructor method must be “__destruct”.
Q: Do we need to call constructors and destructors manually in PHP?
A: No, constructors and destructors are automatically called when an object is created or destroyed and we don’t need to call them manually.
Q: Can we set default values for constructor parameters in PHP?
A: Yes, we can use PHP to establish default values for constructor arguments. For example, if you want the constructor’s second parameter to be optional, you may define a default value like this:
function __construct($name, $color = "Black") {
$this->nick_name = $name;
$this->color = $color;
}
Q: How can a child class call the parent class constructor in PHP?
A: If a child class extends a parent class and wants to call the parent class constructor in its own constructor, it can use the syntax “parent::__construct()” to do so.
Exercises:
- Create a class called “Car” with properties for “make”, “model”, and “year” and a constructor that sets these values when a new object is created.
- Add a method to the “Car” class called “getDetails” that returns the make, model, and year of the car as a string.
- Create a child class called “ElectricCar” that extends the “Car” class and has an additional property for “batteryLife” and a constructor that sets this value.
- Add a method to the “ElectricCar” class called “getBatteryLife” that returns the value of the “batteryLife” property.
- Create an object of the “ElectricCar” class and use the methods you created to display the details and battery life of the car.
Answers:
1.
class Car {
public $make;
public $model;
public $year;
function __construct($make, $model, $year) {
$this->make = $make;
$this->model = $model;
$this->year = $year;
}
}
2.
class Car {
public $make;
public $model;
public $year;
function __construct($make, $model, $year) {
$this->make = $make;
$this->model = $model;
$this->year = $year;
}
function getDetails() {
return $this->make . " " . $this->model . " " . $this->year;
}
}
3.
class ElectricCar extends Car {
public $batteryLife;
function __construct($make, $model, $year, $batteryLife) {
parent::__construct($make, $model, $year);
$this->batteryLife = $batteryLife;
}
}
4.
class ElectricCar extends Car {
public $batteryLife;
function __construct($make, $model, $year, $batteryLife) {
parent::__construct($make, $model, $year);
$this->batteryLife = $batteryLife;
}
function getBatteryLife() {
return $this->batteryLife;
}
}
5.
$electricCar = new ElectricCar("Tesla", "Model S", 2022, 300);
echo $electricCar->getDetails();
echo "<br>";
echo "Battery Life: " . $electricCar->getBatteryLife();