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 A {
  public function test() {
    return "A";
  }
}
class B extends A {
  public function test() {
    return "B";
  }
}
class C extends B {
  public function test() {
    return parent::test();
  }
}
$obj = new C();
echo $obj->test();
AB
BA
CC
DFatal error
Step-by-Step Solution
Solution:
  1. Step 1: Trace class inheritance and method calls

    Class C extends B, which extends A. C's test() calls parent::test(), so it calls B's test().
  2. Step 2: Identify returned value

    B's test() returns "B", so the output is "B".
  3. Final Answer:

    B -> Option A
  4. Quick Check:

    parent:: calls immediate parent method [OK]
Quick Trick: parent:: calls immediate parent, not grandparent [OK]
Common Mistakes:
  • Thinking parent:: calls grandparent method
  • Expecting child's own method output
  • Assuming fatal error due to multiple inheritance

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes