Q:

PHP find output programs (Inheritance) | set 2

0

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.

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

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

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

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

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer 1:

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.

Answer 2:

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.

Answer 3:

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.

Answer 4:

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.

Answer 5:

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now