Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q4 of 15
PHP - Inheritance and Polymorphism
What will be the output of this PHP code?
class A {}
class B extends A {}
$obj = new B();
if ($obj instanceof A) {
    echo 'Yes';
} else {
    echo 'No';
}
AError
BNo
CYes
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Understand class inheritance

    Class B extends A, so B is a subclass of A.
  2. Step 2: Check instanceof behavior

    $obj is an instance of B, which is a subclass of A, so instanceof returns true.
  3. Final Answer:

    Yes -> Option C
  4. Quick Check:

    Subclass instance passes instanceof check = A [OK]
Quick Trick: instanceof returns true for subclasses too [OK]
Common Mistakes:
  • Thinking instanceof only matches exact class
  • Expecting error due to inheritance
  • Ignoring subclass relationship

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes