Constructors and Destructors: PHP OOP


Video tutorial illustrates the use of Constructor and Destructor functions in Object Oriented PHP Programming.

Constructor: A constructor is an initialization routine having the same name as the class name, which is called automatically at the time of object creation.

Earlier to PHP5, constructor function had the same name as the class name. From PHP version 5, constructor functions has this special name __construct
For version compatibility, if we do not have a __construct method, it falls back and looks for the method with the same name as the class name.

Destructor: Destructor method is automatically called when all the references to a class have been unset or fallen out of scope.

Default Constructor:

1
2
3
4
5
6
7
8
9
10
       class Info
{
private $name;
 
function __construct()
{
$this->name = "Microsoft";
echo $this->name;
}
}

A class called Info, which has one private variable called $name;
a default constructor which assigns the value Microsoft to the variable $name and then echos it out.

Parameterized Constructor:

1
2
3
4
5
6
7
8
9
10
11
        class Info
{
private $name;
 
function __construct($text)
{
$this->name = $text;
echo $this->name;
}
      }
$a = new Info("Parameterized Constructor!");

Here we’re passing the parameter “Parameterized Constructor!”, which is then sent to __construct function which is intern assigned to the private variable $name and then echoed out.

Destructor:

1
2
3
4
5
6
7
8
9
10
11
class Info
{
private $name;
 
function __destruct()
{
 
echo "<br />Destructor called!"
       }
 
}

Note: Destructor method do not take parameter/arguments.

Full Code of Class File
info.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
< ?php
    class Info
{
private $name;
 
function __construct($text)
{
$this->name = $text;
echo $this->name;
}
 
function __destruct()
{
echo "<br />Destructor called!"
}
}
?>

Here we have Info class, with 1 private variable and a construct(parameterized) method and a destructor method.

Application File or the Code Processing
index.php

1
2
3
4
5
< ?php
include_once('info.php');
 
$a = new Info("Parameterized Constructor!");
?>

We include the class file(info.php) and then instantiate Info class.
Once it’s instantiated, __construct function is called and all the code inside it will be executed.
At the end of the scope of this class, __destruct method is called and all the code inside destruct method is executed.

Constructors and Destructors: PHP OOP


[youtube https://www.youtube.com/watch?v=xsOO06A8gxU]

YouTube Link: https://www.youtube.com/watch?v=xsOO06A8gxU [Watch the Video In Full Screen.]



Usually programmers connect to the database in the __construct method and then close the database connection inside __destruct method.
But you could configure your database info inside config.php file and then auto-load your database.

I would usually, initialize API’s inside my __construct method. Use the API output inside my other member methods and then release the resources inside __destruct method.

Leave a Reply

Your email address will not be published. Required fields are marked *