Back to Top

Constructor and Destructor in PHP

Constructor and Destructor in PHP

What is Constructor?

Generally Constructor is one kind of method which has the same name as class’s used to initialize the variable of the object at the time of declaration.The constructor is basically OOPs concept in PHP.You also like Type Hinting in PHP5 and PHP Magical Constants.

In PHP4, Constructor is the method which have the same name as class name whereas in PHP5, We have magic method say __construct to call the constructor.The constructor gets called automatically for each object that has got created.It’s may or may not take parameters.

If you declare a function in a class named with two underscores (__) like __construct, this function will be treated as a constructor in PHP when creating an object instance of the class.Like any other function, the constructor can have parameters or default values.

  • Constructor accept parameters, can be assigned to object properties at object creation
  • The constructor can call the class method or other functions
  • Constructor can calls constructors of other classes

NOTE: Constructor should be defined in PUBLIC class.

Basic Example

class Test
	function __construct()
		echo 'Within Constructor';
$obj = new Test();
// OUTPUT: Within Constructor

Note: If the child class defines a constructor is not an implicitly call the constructor of its parent class. To execute the constructor of the parent class, call parent in the constructor of the parent::__construct();

Parameterized Constructor

Parameterized constructor means we pass parameter within in constructor method.Constructor can take arguments so

Let’s see by example.

class const_over
	function __construct($name)
		echo $name;
$obj = new const_over("Bhumi");// Constructor called implictly
// OUTPUT: // Bhumi

NOTE: The constructor is non-static member function.

What is Destructor?

The destructor gets called for each object of the class that is destroyed.It appears as the member function of each class whether it’s defined or not.For destructor, we also have the magic method called __destruct in PHP.

  • Destructor is used to destroy the object at the time, automatic call, the call can not be displayed.
  • Destructors can not have parameters.

The destructor can use to guarantee a proper deinitialize the object of the class and free up the resources acquired by the object.The destructor is also a member function of a class.

Now let’s see mysql connect code which uses both Constructor and destructor.

class Construct_cls {
   var $dbHost,
    function __construct() 
        $this->dbHost = 'localhost';
        $this->dbUser = 'root';
		$this->dbPass = '';
        $this->dbName = 'test';
	function runQuery()
		return $this->rs;
	function __destruct()
$obj = new Construct_cls();
$obj->sql = "SELECT * FROM category";
$result = $obj->runQuery();
$data = mysql_fetch_array($result);

NOTE: The destructor of subclass will not be called implicitly of the parent class, to execute the parent class destructor must call the parent destructor parent::__ destruct().

Here, In above code includes the definition for the destructor function. I have explicitly closed mysql connection using the __destruct function.When the destructor is called, and the object itself is destroyed.

By the way, are you using destructor in your application? Share your viewpoints in the comment section below.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

Basic Github commands

Posted on 3 years ago


How To Use The HTML5 Notification API

Posted on 2 years ago


Hello world

Updated 1 month ago

Nick Carter

In depth Understanding of Laravel Core

Updated 3 months ago


ECMAScript 6 New Features – 2

Updated 1 year ago


The Reader’s Poll – June 2015

Posted on 4 years ago


The Reader’s Poll – August 2014

Posted on 5 years ago


The Reader’s Poll – June 2014

Posted on 5 years ago


The Readers’ Poll – May 2014

Posted on 5 years ago


The Readers’ Poll – August 2013

Posted on 6 years ago