Bird
0
0

What is wrong with this PHP code snippet?

medium📝 Debug Q6 of 15
PHP - Inheritance and Polymorphism
What is wrong with this PHP code snippet?
class Test {}
$instance = new Test();
if (Test instanceof $instance) {
    echo 'Valid';
}
AThe <code>instanceof</code> operator expects an object on the left and a class name or object on the right.
BThe class <code>Test</code> is not defined properly.
CThe <code>instanceof</code> operator cannot be used with variables.
DThe <code>echo</code> statement is invalid inside the if block.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the operands of instanceof

    The left operand must be an object, and the right operand must be a class name string or an object.
  2. Step 2: Identify the error

    Here, Test is a class name, not an object, so using it on the left side is incorrect.
  3. Final Answer:

    The code is invalid because Test instanceof $instance uses a class name on the left side instead of an object. -> Option A
  4. Quick Check:

    Left operand must be an object [OK]
Quick Trick: Left side of instanceof must be an object [OK]
Common Mistakes:
  • Using a class name instead of an object on the left side
  • Confusing the order of operands
  • Using variables incorrectly with instanceof

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes