Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q5 of 15
PHP - Inheritance and Polymorphism
What will be the output of this PHP code?
class ParentClass {
  function show() {
    echo "Parent";
  }
}
class ChildClass extends ParentClass {
  function show() {
    parent::show();
    echo " Child";
  }
}
$obj = new ChildClass();
$obj->show();
AChild
BParent Child
CParent
DChild Parent
Step-by-Step Solution
Solution:
  1. Step 1: Analyze method show() in ChildClass

    ChildClass's show() calls parent::show() which prints "Parent", then prints " Child".
  2. Step 2: Combine outputs

    The output is "Parent Child" as both strings print in order.
  3. Final Answer:

    Parent Child -> Option B
  4. Quick Check:

    parent::method() call prints parent's output first [OK]
Quick Trick: Use parent::method() to include parent's output [OK]
Common Mistakes:
  • Expecting only child's output
  • Mixing order of printed strings
  • Forgetting to call parent method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes