PHP

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

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();
?>
TutorialsLink Curator

This is an official account managed by the team TutorialsLink Community