A constructor is a special function of a class that is automatically executed whenever an object of a class gets instantiated.
A constructor is a special function - this means that a constructor is a function; but its special.
why is it special? : It's special because it is automatically executed or called when an object of a class is created.
It is needed as it provides an opportunity for doing necessary setup operations like initializing class variables, opening database connections or socket connections, etc. In simple terms, it is needed to setup the object before it can be used.
In PHP5 a constructor is defined by implementing the __construct() method. This naming style has been introduced in PHP5.
In PHP4, the name of the constructor was the same name as that of the class. So, for example if you had a class Customer, you would have to implement a function Customer().
PHP5 to be backward complaint also supports the PHP4 rule. However, if you define both; PHP5 will first search for __construct() method and execute it if available, otherwise it will execute the same class name function.
<?php class Customer { private $first_name; private $last_name; private $outstanding_amount; public function __construct() { $first_name = ""; $last_name = ""; $outstanding_amount = 0; } public function setData($first_name, $last_name, $outstanding_amount) { $this->first_name = $first_name; $this->last_name = $last_name; $this->outstanding_amount = $outstanding_amount; } public function printData() { echo "Name : " . $first_name . " " . $this->last_name . "\n"; echo "Outstanding Amount : " . $this->outstanding_amount . "\n"; } } $c1 = new Customer(); $c1->setData("Sunil","Bhatia",0); ?>
In the above example, we create a new object of the Customer class. the 'new' operator is responsible for creating the Customer class. At this point PHP5 searches the Customer class to see if a constructor has been defined. Therefore, it calls the constructor method i.e. __construct() . The __construct() method sets the $first_name and $last_name to blank and sets the $outstanding_amount to zero.
A destructor is a special function of a class that is automatically executed whenever an object of a class is destroyed.
A destructor is a special function - this means that a destructor is a function; but its special
why is it special? : It's special because it is automatically executed or called when an object of a class is destroyed. An object of a class is destroyed when
1.it goes out of scope.
2.when you specifically set it to null.
3.when you unset it or when the program execution is over.
It is needed as it provides an opportunity for doing necessary cleanup operations like unsetting internal class objects, closing database connections or socket connections, etc. In simple terms, it is needed to cleanup the object before it is destroyed.
A PHP5 destructor is defined by implementing the __destruct() method.
In PHP4 however, the concept of a destructor did not exist.
Note :In the above syntax style, $obj_name is a variable in PHP. 'new' is the keyword which is responsible for creating a new instance of ClassName.
<?php class Customer { private $first_name; private $last_name; private $outstanding_amount; public function __construct() { $first_name = ""; $last_name = ""; $outstanding_amount = 0; } public function setData($first_name, $last_name, $outstanding_amount) { $this->first_name = $first_name; $this->last_name = $last_name; $this->outstanding_amount = $outstanding_amount; } public function printData() { echo "Name : " . $first_name . " " . $last_name . "\n"; echo "Outstanding Amount : " . $outstanding_amount . "\n"; } } class Order { private $order_id; private $customer; public function __construct($order_id, $customer) { $this->order_id = $order_id; $this->customer = $customer; } public function __destruct() { unset($this->order_id); unset($this->customer); } } $order_id = "L0001"; $c1 = new Customer(); $c1->setData("Sunil","Bhatia",0); $o = new Order($order_id, $c1); ?>
In the above example, we create a new object of the Order class. The argument constructor of the Order class takes two parameters i.e. $order_id and $customer object. After the program completes its execution, the object goes out of scope because the program stops execution and hence the destructor is automatically called.
After editing Click here: |
Output:
|
Hello World
|