ARTICLE

Constructor and Destructor in PHP

Posted by Sharad Gupta Articles | PHP January 29, 2013
In this article I explain how to create a constructor and release memory by a destructor.
Reader Level:

Introduction

The constructor and destructor is a feature of Object Oriented Programming (OOP). And this concept is implemented in PHP5. Now in this article, I explain "constructor" and "destructor" separately.

Constructor

The constructor is an "OOP" feature. PHP 5 has OOP capability and gives the authority to developers to declare a constructor method for classes. I will first explain what a constructor is. The answer is "A constructor is a special type of method that is automatically called when you create a new instance of the class". The OOP constructor has a number of advantages, they are:

  • Constructors can accept parameters, that are assigned to specific object fields at creation time.
  • Constructors can call class methods or other functions.
  • Class constructors can call on other constructors, including those from the parent class.
     

Example

In the following code, the parent class (in other words Myclass) has a constructor. It will not be called implicitly because its sub class (in other words Baseclass) defines a constructor (in PHP the parent class constructor is not called implicitly if the child class defines a constructor). Whenever an object of the child class is created. The sub class constructor will be called automatically.


<?
php
class
Myclass
{

function
__construct()
{

print
"Parent class constructor.</br>";
}
}

class
Baseclass extends Myclass
{

function
__construct()
{

parent
::__construct();
print
" Child Class constructor\n";
}

$obj = new Myclass();

$obj = new Baseclass();

 

?>

Output

constructor-in-php.jpg

Destructor

The destructor is another feature of "OOP". The destructor concept was introduced in PHP5 and it is conceptually similar to that of other object oriented languages. The destructor method will be called as soon as there are no other references to a particular object or when the object has been explicitly destroyed.

Example

<?
php
class
MyClass
{

function __construct()
{

print "In constructor";

$this->name = "MyClass obj explicity";

}

 

function __destruct()
{

print "Destroying " . $this->name . "\n";

}

}

 

$obj = new Myclass(); 

?>

Output

destructor-in-php.jpg

Login to add your contents and source code to this article
post comment
     

Oky Thank u Sharad

Posted by Nitin Bhardwaj Feb 04, 2013

Hi Nitin , if you want to implement copy constructor in PHP,then you can use clone method to implement copy constructor facility in PHP.<?php class Myclass { static $instances=0; public $instance; function __construct() { $this->instance=++self::$instances; } function __clone() { $this->instance=++self::$instances; } } class Baseclass { public $object1; public $object2; function __clone() { $this->object1=clone $this->object1; } } $obj = new Baseclass(); $obj->object1=new Myclass(); $obj->objec21=new Myclass(); $obj2=clone $obj; echo "<pre>"; print("Orignal object:\n"); print_r($obj); print("clone object:\n"); print_r($obj2); ?>

Posted by Sharad Gupta Feb 04, 2013

Hi Sarad, Can we create copy constructor in PHP?

Posted by Nitin Bhardwaj Feb 04, 2013

thanks Prabhakar

Posted by Sharad Gupta Feb 01, 2013

Nice Explain...

Posted by Prabhakar Maurya Feb 01, 2013
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter