PHP program to call base class destructor from the derived class

Here, we are going to learn how to call base class destructor from the derived class in PHP?
Submitted by Nidhi, on November 20, 2020

Here, we will call the destructor of the parent class from the destructor of the child class, here we need to use the parent keyword with :: (scope resolution operator).

Program/Source Code:

The source code to call base class destructor from derived class is given below. The given program is compiled and executed successfully.

<?php
//PHP program to call base class destructor 
//from the derived class.
class Base
{
    function __destruct()
    {
        echo "Base:destructor  called<br>";
    }
}

class Derived extends Base
{
    function __destruct()
    {
        echo "Derived:destructor  called<br>";
        parent::__destruct();
    }
}

$dObj = new Derived();
?>

Output:

Derived:destructor called
Base:destructor called

Explanation:

In the above program, we created two classes Base and Derived. Here, we inherited the Base class into the derived class using the extends keyword.

Both Base and Derived classes contain destructors. Here, we called the parent class destructor from child class destructor using the parent keyword.

At last, we created the object $dObj of Derived class then the destructor of the derived class gets called that will call the destructor of Base class using parent keyword that will print the appropriate message on the webpage.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.