Pages - পৃষ্ঠাসমূহ

Object oriented php tutorial - Constructor & Destructor

PHP 5 allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.
Hide Example Show Example



This tutorial contains some parts of tuorial

Object Oriented PHP - Class and object
Object Oriented PHP - Interface and abstractor
Object Oriented PHP - Constructor and destructor
Object Oriented PHP - Inheritance
Object Oriented PHP - Visivility
Object Oriented PHP - Some common keywords
some magic words in php


Class constructor and Class destructor



Constructor methods are automatic run when a class is instantiate. Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.
<?php
 class Vehicle {
 
  public $brand="Motorola";
  private $color="Black";
  protected  $price="$1200";
  
  function __construct() {
     echo "Welcome to vehicle shop";
  }
 }

 class Bike extends vehicle{
    function __construct() {
     parent::__construct();
   echo "The subclss constractor";
    }
 }

 $obj = new Vehicle();
 $obj = new Bike();
?> 
Now we see a constructor with parameter. When we create a parameterized contructor we need to create an instantiate with parameters value.
<?php
 class Vehicle {
 
  public $brand="Motorola";
  private $color="Black";
  protected  $price="$1200";
  
  function __construct(&a) {
     $a=$this->price;
     echo "Price of the bike is : ".$a;
  }
 } 

 $obj = new Vehicle("1200");
 
?> 
After creating a construct we need this to destruct after working this. PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++. The destructor method will be called as soon as there are no other references to a particular object, or in any order during the shutdown sequence.
<?php
 class Vehicle {
    function __construct() {
     echo  "Constructor loaded\n";
     $this->name = "Vehicle";
    }

    function __destruct() {
     echo "Destroying " . $this->name . "\n";
    }
 }

 $obj = new Vehicle();
?> 
Like constructors, parent destructors will not be called implicitly by the engine. In order to run a parent destructor, one would have to explicitly call parent::__destruct() in the destructor body.
The destructor will be called even if script execution is stopped using exit(). Calling exit() in a destructor will prevent the remaining shutdown routines from executing.
Destructors called during the script shutdown have HTTP headers already sent. The working directory in the script shutdown phase can be different with some SAPIs (e.g. Apache). Attempting to throw an exception from a destructor (called in the time of script termination) causes a fatal error.

In previous PHP version the construcor is define by class name. But there has no way to call any destructor.
<?php
 class Vehicle {
  public function Vehicle() {
   // treated as constructor in PHP 5.3.0-5.3.2
   // treated as regular method as of PHP 5.3.3
  }
 }
?>

2 comments: