Bird
0
0

Consider this PHP code:

hard📝 Application Q9 of 15
PHP - Inheritance and Polymorphism
Consider this PHP code:
class X {
  public function foo() {
    return "X";
  }
}
class Y extends X {
  public function foo() {
    return "Y";
  }
}
class Z extends Y {
  public function foo() {
    return parent::foo() . parent::foo();
  }
}
$obj = new Z();
echo $obj->foo();

What is the output?
AYY
BXY
CXX
DFatal error
Step-by-Step Solution
Solution:
  1. Step 1: Understand parent::foo() calls

    In class Z, parent::foo() calls Y's foo(), which returns "Y".
  2. Step 2: Evaluate concatenation

    Z concatenates two calls to parent::foo(), so output is "Y" . "Y" = "YY".
  3. Final Answer:

    YY -> Option A
  4. Quick Check:

    Multiple parent:: calls repeat parent method output [OK]
Quick Trick: parent:: calls immediate parent method each time [OK]
Common Mistakes:
  • Thinking parent::foo() calls grandparent
  • Expecting different outputs from repeated calls
  • Assuming error due to repeated parent calls

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes