PHP Syntax OOP Classes Components Methods Predefined Constructor Call Parent Constructor

 

- Info:

- Constructor is method which is called when object of the class is created using keyword new.

- Constructor must be named as __construct().

- Constructor is suitable for initializing object.

- If child doesn't have constructor and parent does, ti will be automaticly called.

- Parent constructors are not called implicitly, instead explicit call to parent::__construct() is needed.

- This tutorial is based on http://php.net/manual/en/language.oop5.decon.php.

 

- Additioanl Tutorials:

- PHP Syntax Terms Classes Ineritance Call Parent Constructor

 

- Example:

- Creat PHP Console Application Test.php.

 

Test.php

<?php

class BaseClass {

function __construct() {

print "In BaseClass constructor\n";

}

}

class SubClass extends BaseClass {

function __construct() {

parent::__construct();

print "In SubClass constructor\n";

}

}

$obj = new BaseClass();

$obj = new SubClass();

?>