Friday, 6 November 2015

CONSTRUCTOR-DESTRUCTOR

22:29

Constructor   Functions   :

Constructor Functions are special type of functions which are called automatically whenever an object is created. So we take full advantage of this behaviour, by initializing many things through constructor functions.
PHP provides a special function   called __construct()   to define a constructor. You can pass as many as arguments you like into the constructor function.
Following example will create one constructor for Books class and it will initialize price and title for the book at the time of object creation.

function __construct( $par1, $par2 ){
   $this->price = $par1;
   $this->title = $par2;
}


Destructor   :

Like a constructor function you can define a destructor function using function __destruct().  You can release all the resourceses with-in a destructor.
Example for constructor & Destructor  :

<?php
class   example
{

function   __construct()
{

echo "constructor is called & object  is created";
echo "<br>";

}

Function  __destruct()
{

echo "destructor is called & object is free";

}
}
$obj=new  example();

?>






How to call parent class   constructor     parent::__construct();   :
<?php
class BaseClass {
   function __construct() {
       print "In BaseClass constructor\n";
   }
}

class SubClass extends BaseClass {
   function __construct()
   {
       parent::__construct();
       print "In SubClass constructor\n";
   }
}

//$obj = new BaseClass();
$obj = new SubClass();
?>


A constructor and a destructor are special functions which are automatically called when an object is created and destroyed. The constructor is the most useful of the two, especially because it allows you to send parameters along when creating a new object, which can then be used to initialize variables on the object. Here's an example of a class with a simple constructor:
class Animal
{
    public $name = "No-name animal";
    
    public function __construct()
    {
        echo "I'm alive!";        
    }
}
As you can see, the constructor looks just like a regular function, except for the fact that it starts with two underscores. In PHP, functions with two underscore characters before the name usually tells you that it's a so-called magic function, a function with a specific purpose and extra functionality, in comparison to normal functions. So, a function with the exact name "__construct", is the constructor function of the class and will be called automatically when the object is created. Let's try doing just that: 

$animal = new Animal(); 

With just that line of code, the object will be created, the constructor called and the lines of code in it executed, which will cause our "I'm alive!" line to be outputted. However, as mentioned previously, a big advantage of the constructor is the ability to pass parameters which can be used to initialize member variables. Let's try doing just that:
<?php
class Animal
{
    public $name = "No-name animal";
    
    public function __construct($name)
    {
        $this->name = $name;
    }
}

$animal = new Animal("Bob the Dog");
echo $animal->name;
?>
Declaring the constructor with parameters is just like declaring a regular function, and passing the parameter(s) is much like calling a regular function, but of course with the "new" keyword that we introduced earlier. A constructor can have as many parameters as you want.

Destructors

A destructor is called when the object is destroyed. In some programming languages, you have to manually dispose of objects you created, but in PHP, it's handled by the Garbage Collector, which keeps an eye on your objects and automatically destroys them when they are no longer needed. Have a look at the following example, which is an extended version of our previous example:
<?php
class Animal
{
    public $name = "No-name animal";
    
    public function __construct($name)
    {
        echo "I'm alive!";    
        $this->name = $name;
    }
    
    public function __destruct()
    {
        echo "I'm dead now :(";
    }
}

$animal = new Animal("Bob");
echo "Name of the animal: " . $animal->name;
?>
As you can see, the destructor is just like a constructor, only the name differs. If you try running this example, you will see first the constructor message, then the name of the animal that we manually output in the last line, and after that, the script ends, the object is destroyed, the destructor is called and the message about our poor animal being dead is outputted

Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 comments:

Post a Comment

PHP Training Mumbai, PHP MySQL Training Mumbai, PHP Course Mumbai, PHP Institute Mumbai

 

© 2013 PHP TRAINING IN MUMBAI BY OM SIR . All rights resevered. Designed by OM SIR

Back To Top