Surfe.be - passive income

What are __construct __destruct and Polymorphism in PHP

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. 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. The OOP constructor has a number of advantages, they are:

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

How to declare a constructor       

class i_am_class{

	public function __construct(){
		//Code goes here
	}
}

Let’s see the example

class i_am_class{

	public function __construct(){
		echo "Constructor Created Successfully";
	}
}

$object = new i_am_class();

OUTPUT :

Constructor Created Successfully

Because when we create an instance($object)  of class i_am_class then the __construct() will be automatically loaded. Hope you have understood. 

Value Initialize with constructor  


class i_am_class{
    public $name;

	public function __construct($name){
		$this->name = $name; // Initializing value with parameter
	}

	public function Display(){
		echo "My name is {$this->name}";
	}
}

$object = new i_am_class("John");
$object->Display();

Look here we initialized value using constructor and we can access this value inside class from any method . Note again: when we create an object of  class then constructor will be automatically loaded whether we create it or not , it does’nt matter. But we can create for our purposes.    

 

Destructor

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:

 

class i_am_class{
    public $name;

	public function __construct($name){
		$this->name = $name;
	}

	public function Display(){
		echo "My name is {$this->name}";
	}

	public function __destruct(){
		mysql_close();
	}
}

$object = new i_am_class("John");
$object->Display();

Look here we have used a destructor to close our database connection . Why ? Cause you already known that a destructor is called when a object’s work is finished . So when the purposes of i_am_class is ended then we can call destructor for our next term like database closing or any other purpose what you would like to do. Hope you have understood.

Polymorphism

Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object . When we start to extend classes and add functionality to them which wasn't there previously , and even override existing methods this procedure is called polymorphism . The basis of polymorphism is inheritance and overriden methods . Let’s see the example .

class i_am_class { //Parent class
    public $name;

	public function __construct($name){
		$this->name = $name;
	}

	public function Display(){
		echo "My name is {$this->name}";
	}
}

class  i_am_sub_class extends i_am_class { //Subclass

	public $age = "25"; // add new functionality which was not in parent class

	public function Display(){ //override method which was declared in parent class
		echo "My name is {$this->name} and age is {$this->age}";
	}
}

 $subclass = new i_am_sub_class();

 if ($subclass instanceof i_am_class) { //checking whether it was inherited or not
 	echo "Inherited";
 }

Hope you will understand all the topics . If you like this tutorial or find any bugs then please leave a comments and don’t forget to share .

 

Leave a comments

LET'S SOCIALITE

Recent Tweets

RECOMMENDED POST