Concepts of Constructors, Destructors and its types in PHP

Easy Solution Web: constructors and destructors in php

In this section, we will talk about constructors, destructors, their types and some important characteristics

Constructors

Let’s start with some common characteristics of Constructor

  1. We can define a constructor method by defining the __construct method inside the class.
  2. It runs only one time while creating an Object from the Class.
  3. It is the suitable place to initialize your class member variables.
  4. We should always use the __construct method instead of old-style class_name() constructor method as it is going to be deprecated in PHP 7.
  5. To call a parent class constructor in child class you can use parent::__construct().
  6. Parent class constructor is not implicitly in the child class, you need to call it as above explained in child class if the constructor is not defined private.

Syntax to define constructor in PHP

class Person{
public $name;
public $gender; 
public $address; 
public $height; 
public $color; 
public $race; 
public function __construct(){ 
$this->race = "human"; 
} 
}

Old style constructor in PHP treated as constructor in till PHP 5.3.2 now treated as normal method inside a class

class Person{
public $name;
public $gender; 
public $address; 
public $height; 
public $color; 
public $race; 
public function Person(){ 
$this->race = "human"; 
} 
}
how to call parent class constructor inside child class constructor
class Person{
public $name; 
public $gender; 
public $address; 
public $height; 
public $color; 
public $race; 
public function __constuct(){ 
$this->race = "human"; 
} 
} 
class Employee extends Person{ 
public function __constuct(){ 
parent::__construct(); // it will Person class constructor 
} 
} 
$emp1 = new Employee(); // it will call employee and parent class constructor both.

Types of constructors we have in PHP

  1. Default.
  2. Parameterized
  3. Copy Constructor

 

Default Constructors

class Person{
public $name; 
public $gender; 
public $address; 
public $height; 
public $color; 
public $race;
// if __construct() is not defined inside the class then the implicit constructor will called which is a Default Constructor 
}

Parameterized Constuctors

class Person1{
public $name;
public $gender;
public $address;
public $height;
public $color;
public $race;
public function __construct(){ // parameterized constructor with no-argument
$this->race = "human";
} 
}
$perObj1 = new Person1(); // parameterized constructor with no-argument
class Person2{
public $name;
public $gender;
public $address;
public $height;
public $color;
public $race;
public function __construct($name){ // parameterized constructor with name argument
$this->race = "human";
$this->name = $name; 
} 
}
$perObj2 = new Person2("mike"); // parameterized constructor with name argument

 

Copy Constructors

class Person1{
public $name;
public $gender;
public $address;
public $height;
public $color;
public $race;
public function __construct($name){ // parameterized constructor with name argument
$this->race = "human";
$this->name = $name; 
}
public function __clone(){ 
echo "do something";
} 
}
$perObj2 = new Person1("mike"); // parameterized constructor with name argument
$perObj3 = clone $perObj2; // copy constructor initialize $perObj3 with the values of $perObj2
echo "<br/>".$perObj3->name;
Output : 
do something
mike

Destructors

Characteristics of Destructor:

  1. It also gets called only once in the lifetime of an Object.
  2. It gets called at the end of execution of PHP script, function or while calling exit(); in the PHP script.
  3. It is used to free up memory space consumed by Object.
  4. Its called implicitly by the garbage collector once it knows that the particular object is of no use. So garbage collector calls it to free memory space consumed by Desctructor.
  5. To define a destructor we need to define a __destruct() method inside the class.
  6. For a Class, the __destruct method will be only one.
class Person1{
public $name;
public $gender;
public $address;
public $height;
public $color;
public $race;
public function __construct($name){ 
$this->race = "human";
$this->name = $name; 
}
function __destruct() { //syntax to define a destruct
    print "Destroying " . $this->name . "\n";
} 
}