PHP TUTORIALPHP TutorialPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP StringsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP String FunctionsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP AdvancedPHP Include and RequirePHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms FinishingPHP OOPPHP OOP IntroductionPHP OOP ClassesPHP OOP PropertiesPHP OOP ObjectsPHP OOP MethodsPHP OOP $this KeywordPHP OOP Constructors and DestructorsPHP OOP VisibilityPHP OOP InheritancePHP OOP Abstract Classes and MethodsPHP OOP InterfacesPHP OOP TraitsPHP OOP ConstantsPHP OOP StaticPHP OOP Namespaces

PHP OOP Constructors and Destructors

  • Constructors: Constructors are called when an object is created from a class.
  • Destructors: Destructors are called when an object destructs. Usually, it is when the script ends.

Constructors

A constructor is a public method which is named as __construct.


class Example {
	public function __construct() {
		// your constructor function
		echo "Hello World";
	}
}

The __construct() method will be called once when you create an object from the class.


$example = new Example(); // will echo "Hello World"

Let's re-create the example in the previous lesson using a constructor. Our __construct() method will have two arguments, $name and $color. These arguments should be sent when we create the object.

PHP Constructor Example


<?php
class House {
	public $name;
	public $color;
	public function __construct($name, $color) {
		$this -> name = $name;
		$this -> color = $color;
	}
	public function echoData() {
		echo "The color of the {$this -> name} is {$this -> color}";
	}
}
$blackHouse = new House("John's House", "black");
$blackHouse -> echoData();

Run Example ››
The __construct() method should always have public visibility

Tip: When you don't have a constructor...

When you don't have a constructor (or you have a constructor without arguments), you can create the object from the class without parentheses.


$house = new House;

But, if you have a constructor with arguments, you should send the values for those arguments when you create it.

Destructor

A destructor is called when the object is destructed or the script is stopped or exited.

PHP Destructor Example


<?php
class House {
	public $name;
	public $color;
	public function __construct($name, $color) {
		$this -> name = $name;
		$this -> color = $color;
	}
	public function __destruct() {
		echo "The color of the {$this -> name} is {$this -> color}";
	}
}
$blackHouse = new House("John's House", "black");

Run Example ››

In the above example, the __destruct() method will be called at the end of the execution of the script.

Note: The __desctructor method will only be called when the current PHP script is fully executed by reaching the last line of it, or by exiting it using exit() or die() functions.

Conclusion

In the real world, constructors and destructs are very useful. In the upcoming chapters, we will be using these methods very often.

Facebook Twitter