codescracker


php

PHP Constructor and Destructor



« Previous Tutorial Next Tutorial »


Here you will learn about constructor and desctructor, an object oriented concept in PHP with example.

PHP Constructor

Constructor in PHP refers to a special type of function which will be called automatically whenever there is an object formation from a class.

PHP Constructor Syntax

Here is the general form to declare constructor in PHP.

void __construct()

Here __construct is the keyword used in declaring constructor method in PHP.

PHP Constructor Example

Let's take an example to understand about constructor in a practical manner. The example given below demonstrates constructor in PHP.

<!DOCTYPE html>
<html>
<head>
	<title>Constructor Example in PHP</title>
</head>
<body>
<?php
	// PHP Constructor Example Code - CodesCracker
	class MyBaseClass
	{
		function __construct()
		{
			echo "I am in MyBaseClass constructor<br/>";
		}
	}
	class AnotherClass extends MyBaseClass
	{
		function __construct()
		{
			// this will call the parent constructor, which is in MyBaseClass class
			parent::__construct();
			// after the above, below will be printed
			echo "I am in AnotherClass constructor<br/>";
		}
	}
	class AnotherThirdClass extends MyBaseClass
	{	
		// this will inherit the constructor of MyBaseClass class
	}
	// In MyBaseClass constructor
	$objct = new MyBaseClass();
	// In MyBaseClass and AnotherClass constructor
	$objct = new AnotherClass();
	// In MyBaseClass constructor
	$objct = new AnotherThirdClass();
?>
</body>
</html>

As you can see from the above constructor example, when you create an object of a class that includes or contains constructor, then that constructor or the content inside that constructor will automatically be executed.

Here, you can see when you create an object say $objct of the class named MyBaseClass, then the constructor inside that class will automatically be called and executed their content which is

echo "I am in MyBaseClass constructor<br/>";

and this will print the text, I am in MyBaseClass constructor and then breaks the line using the br tag of HTML. Same rule apply for all the next object created for classes.

Here is the sample output produced by the above constructor example code in PHP.

php constructor

PHP Destructor

Destructor in PHP refers to a special type of function which will be called automatically whenever an object is deleted or goes out of scope

PHP Destructor Syntax

Below is the general form to declare destructor in PHP.

void __destruct(void)

Here __destruct is the keyword used to declare destructor method in PHP.

PHP Destructor Example

Here is a simple example illustrating destructor in PHP.

<!DOCTYPE html>
<html>
<head>
	<title>Destructor Example in PHP</title>
</head>
<body>
<?php
	// PHP Constructor Example Code - CodesCracker
	class MyDestructableClass
	{
		function __construct()
		{
			echo "I am in MyDestructableClass constructor<br/>";
			$this->name = "MyDestructableClass";
		}
		function __destruct()
		{
			echo "Destroying... ".$this->name."<br/>";
		}
	}
	class AnotherClass extends MyDestructableClass
	{
		function __construct()
		{
			// this will call the parent constructor, which is in MyDestructableClass class
			parent::__construct();
			// after the above, below will be printed
			echo "I am in AnotherClass constructor<br/>";
		}
	}
	class AnotherThirdClass extends MyDestructableClass
	{
		// this will inherit the constructor of MyDestructableClass class
	}
	// In MyDestructableClass constructor
	$objct = new MyDestructableClass();
	// In MyDestructableClass and AnotherClass constructor
	$objct = new AnotherClass();
	// In MyDestructableClass constructor
	$objct = new AnotherThirdClass();
	echo "<hr/>";
?>
</body>
</html>

Here is the sample output produced by the above destructor example code in PHP.

php destructor

PHP Constructor and Destructor Example

Let's take more example on constructor and destructor, an object-oriented concept, available in PHP.

<!DOCTYPE html>
<html>
<head>
	<title>Constructor and Destructor Example in PHP</title>
</head>
<body>
<?php
	class MyConstructorDestructor
	{
		private $name;
		private $link;
		public function __construct($name)
		{
			$this->name = $name;
		}
		public function linkSettingFunction(MyConstructorDestructor $link)
		{
			$this->link = $link;
		}
		public function __destruct()
		{
			echo 'Destroying...: ', $this->name."<br/>";
		}
	}
	// now create two object of MyConstructorDestructor class
	$object1 = new MyConstructorDestructor('MyConstructorDestructor 1');
	$object2 = new MyConstructorDestructor('MyConstructorDestructor 2');

	// now make them point to each other with following two line of code
	$object1->linkSettingFunction($object2);
	$object2->linkSettingFunction($object1);
	
	// then destroy global references to them with following two line of code
	$object1 = null;
	$object2 = null;
	
	// as the global references to $object1 and $object2 is destroyed
	// therefore there is no any way to access MyConstructorDestructor 1 and MyConstructorDestructor 2
	// but here, then are not destroyed, therefore, they still consume memory space
	
	// to see the difference when explicitly calling the GC
	// then just uncomment the below function
	// gc_collect_cycles();
	
	// now again create two objects of the class, MyConstructorDestructor, but this time 
	// don't set their internal MyConstructorDestructor references
	// so nothing point to them except $object1 and $object2 point to them:
	$object1 = new MyConstructorDestructor('MyConstructorDestructor 3');
	$object2 = new MyConstructorDestructor('MyConstructorDestructor 4');
	$object1 = null;
	$object2 = null;
	echo 'End of script';
?>
</body>
</html>

Here is the sample output produced by the above constructor and destructor example code in PHP.

php constructor destructor

Now remove the comment or uncomment the below line of code.

// gc_collect_cycles();

Important - Calling the PHP function, named, gc_collect_cycles() does have some speed overhead, therfore don't use without any reason or if not required.

Here is the simple program that will come out after uncommenting the above function.

<!DOCTYPE html>
<html>
<head>
	<title>Constructor and Destructor Example in PHP</title>
</head>
<body>
<?php
	class MyConstructorDestructor
	{
		private $name;
		private $link;
		public function __construct($name)
		{
			$this->name = $name;
		}
		public function linkSettingFunction(MyConstructorDestructor $link)
		{
			$this->link = $link;
		}
		public function __destruct()
		{
			echo 'Destroying...: ', $this->name."<br/>";
		}
	}
	$object1 = new MyConstructorDestructor('MyConstructorDestructor 1');
	$object2 = new MyConstructorDestructor('MyConstructorDestructor 2');
	$object1->linkSettingFunction($object2);
	$object2->linkSettingFunction($object1);
	$object1 = null;
	$object2 = null;
	gc_collect_cycles();
	$object1 = new MyConstructorDestructor('MyConstructorDestructor 3');
	$object2 = new MyConstructorDestructor('MyConstructorDestructor 4');
	$object1 = null;
	$object2 = null;
	echo 'End of script';
?>
</body>
</html>

Now this will produce the following output

php constructor destructor example

Let's take a last and simplest example on constructor in PHP.

<!DOCTYPE html>
<html>
<head>
	<title>Constructor and Destructor Example in PHP</title>
</head>
<body>
<?php
	class WhatTutorialILike
	{
		function __construct($tutorialName)
		{
			$this->name = $tutorialName;
			echo "I just love to read ".$this->name." on CodesCracker<br/>";
			// you can also convert the above two line of code with 
			// only one line, which will be
			// echo "I just love to read ".$tutorialName." on CodesCracker<br/>";
		}
	}
	$objct = new WhatTutorialILike("PHP");
	$objct = new WhatTutorialILike("HTML");
	$objct = new WhatTutorialILike("CSS");
	$objct = new WhatTutorialILike("Java");
	$objct = new WhatTutorialILike("C");
	$objct = new WhatTutorialILike("C++");
?>
</body>
</html>

The above example code on PHP constructor will produce the output given below:

constructor destructor example php

« Previous Tutorial Next Tutorial »




Tools
Calculator

Quick Links
Signup - Login - Give Online Test