PHP find output programs (Inheritance) | set 2

Find the output of PHP programs | Inheritance | Set 2: Enhance the knowledge of PHP Inheritance by solving and finding the output of some PHP programs.
Submitted by Nidhi, on January 28, 2021

Question 1:

<?php
    class A
    {
        function __construct()
        {
            echo "Class A constructor called<br>";
        }
    
        function fun1()
        {
            echo "Fun1() called<br>";
        }
    
    }
    
    class B extends A
    {
        function __construct()
        {
            echo "Class B constructor called<br>";
        }
    
        function fun2()
        {
            echo "Fun2() called<br>";
        }
    }
    
    $obj = new B();
    $obj->fun2();
?>

Output:

Class B constructor called
Fun2() called

Explanation:

In the above program, we created two classes A and B. Class A contains a member functions fun1() and constructor. class B contains a member functions fun2() and constructor. Then we created the object of class B. Then it will call the constructor of class B but it will not call the constructor of parent class like C++. And finally, we called function fun2() that will print "Fun2() called" on the webpage.

Question 2:

<?php
    class A
    {
        function __construct()
        {
            echo "Class A constructor called<br>";
        }
    
        function fun1()
        {
            echo "Fun1() called<br>";
        }
    
    }
    
    class B extends A
    {
        function __construct()
        {
            parent::__construct();
            echo "Class B constructor called<br>";
        }
    
        function fun2()
        {
            echo "Fun2() called<br>";
        }
    }
    
    $obj = new B();
    $obj->fun2();
?>

Output:

Class A constructor called
Class B constructor called
Fun2() called

Explanation:

In the above program, we created two classes A and B. Class A contains a member functions fun1() and constructor. class B contains a member functions fun2() and constructor. Then we created the object of class B. Then it will call the constructor of class B and we called parent class constructor using the parent keyword explicitly. And finally, we called function fun2() that will print "Fun2() called" on the webpage.

Question 3:

<?php
    class A
    {
        function A()
        {
            echo "Class A constructor called<br>";
        }
    
        function fun1()
        {
            echo "Fun1() called<br>";
        }
    
    }
    
    class B extends A
    {
        function B()
        {
            parent::__construct();
            echo "Class B constructor called<br>";
        }
    
        function fun2()
        {
            echo "Fun2() called<br>";
        }
    }
    
    $obj = new B();
    $obj->fun2();
?>

Output:

Class A constructor called
Class B constructor called
Fun2() called

Explanation:

In the above program, we created two classes A and B. Class A contains a member functions fun1() and constructor. Class B contains a member functions fun2() and constructor. Then we created the object of class B. Then it will call the constructor of class B and we called parent class constructor using the parent keyword explicitly. And finally, we called function fun2() that will print "Fun2() called" on the webpage.

Question 4:

<?php
    class A
    {
        function A()
        {
            echo "Class A constructor called<br>";
        }
    
        function fun1()
        {
            echo "Fun1() called<br>";
        }
    
    }
    
    class B extends A
    {
        function B()
        {
            parent::__construct();
            echo "Class B constructor called<br>";
        }
    
        function fun2()
        {
            echo "Fun2() called<br>";
        }
    }
    class C extends B
    {
        function C()
        {
            parent::B();
            echo "Class C constructor called<br>";
        }
    
        function fun3()
        {
            echo "Fun3() called<br>";
        }
    }
    
    $obj = new C();
    $obj->fun3();
?>

Output:

Class A constructor called
Class B constructor called
Class C constructor called
Fun3() called

Explanation:

In the above program, we created three classes A, B, and C. Class A contains a member functions fun1() and constructor. Class B contains a member functions fun2() and constructor. And Class C contains a member functions fun3() and constructor.

Then we created the object of class C. Then it will call the constructor of class C and it will call the constructor of class B using the parent keyword explicitly. And then class A constructor will be called.

Then finally we called function fun3() that will print "Fun3() called" on the webpage.

Question 5:

<?php
    class A
    {
        function fun1()
        {
            echo "Fun1() called<br>";
        }
        function __destruct()
        {
            echo "Class A destructor called<br>";
        }
    }
    
    class B extends A
    {
        function fun2()
        {
            echo "Fun2() called<br>";
        }
        function __destruct()
        {
            echo "Class B destructor called<br>";
            parent::__destruct();
        }
    
    }
    
    $obj = new B();
    $obj->fun2();
?>

Output:

Fun2() called
Class B destructor called
Class A destructor called

Explanation:

In the above program, we created two classes A and B. Class A contains a member functions fun1() and destructor. Class B contains a member functions fun2() and destructor. Then we created the object of class B and called method fun2(). The destructor of class B called when the scope of the object gets finished then the destructor of class A called using parent keyword explicitly.






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.