PHP Object Oriented Programming Part-4(Constructor Method and Destructor Methods)

What is the Constructor Method in PHP?

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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:

READ  PHP Image Upload Using jQuery and MySQL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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.