ADVERTISEMENT
ADVERTISEMENT

PHP find output programs (Inheritance) | set 1

Find the output of PHP programs | Inheritance | Set 1: 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 fun1()
        {
            echo "Fun1() called<br>";
        }
    }
    class B extends A
    {
        function fun2()
        {
            echo "Fun2() called<br>";
        }
    }
    
    $obj = new B();
    $obj->fun1();
    $obj->fun2();
?>

Output:

Fun1() called
Fun2() called

Explanation:

In the above program, we created a class A that contains a member function fun1(), and then inherit class A into class B using extends keyword, class B contains a member function fun2(). Then we created an object of class B and called functions fun1() and fun2().

Question 2:

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

Output:

PHP Fatal error:  Uncaught Error: Call to undefined function fun1() 
in /home/main.php:13
Stack trace:
#0 /home/main.php(19): B->fun2()
#1 {main}
  thrown in /home/main.php on line 13

Explanation:

The above program will generate syntax error, because called inherited function fun1() inside the fun2() of class B. We did not use correct syntax to call inherited member function in child class. To access the inherited function, we need to use $this.

The proper syntax is given below:

$this->fun1();

Question 3:

<?php
    class A
    {
        protected function fun1()
        {
            echo "Fun1() called<br>";
        }
    }

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

Output:

Fun1() called
Fun2() called

Explanation:

In the above program, we defined two classes A and B. Here, A is a superclass and B is a Child class. Class A contains a protected member function, which is called inside the fun2() member function of class B. Then finally created object $obj of class B and called method fun2(), and fun2() called fun1() inside it.

Question 4:

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

Output:

PHP Fatal error:  Uncaught Error: Call to protected method A::fun1() 
from context '' in /home/main.php:19
Stack trace:
#0 {main}
  thrown in /home/main.php on line 19

Explanation:

The above program will generate syntax error because protected members can be accessed inside the same as well as child class. But protected members cannot be accessed outside the class.

Question 5:

<?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:

Fun2() called

Explanation:

In the above program, we created two classes A and B. Class A contains two member functions construct() and fun1() and class B contains construct() and fun2().

The construct() method is looking like a constructor, but it is a simple method of a class, if we want to define a constructor then we need to using magic function __construct() or the member function with the class name.



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

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.