How to call a base class constructor from the derived class in PHP | PHP programs
Jan 28, 2022
PHP,
2050 Views
How to call a base class constructor from the derived class in PHP | PHP programs
How to call a base class constructor from the derived class in PHP | PHP programs
We will call the constructor of the parent class from the constructor of the child class, here we need to use the parent keyword with :: (scope resolution operator).
<?php
class Base
{
function __construct()
{
echo "Base:constructor called<br>";
}
}
class Derived extends Base
{
function __construct()
{
parent::__construct();
echo "Derived:constructor called<br>";
}
}
$dObj = new Derived();
?>


