Bird
0
0

Given the code below, what will be the output?

medium📝 Predict Output Q13 of 15
PHP - Inheritance and Polymorphism

Given the code below, what will be the output?

class Vehicle {}
class Car extends Vehicle {}

function startEngine(Vehicle $v) {
    echo get_class($v);
}

$car = new Car();
startEngine($car);
AError: Type mismatch
BVehicle
CCar
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Understand type hinting with inheritance

    The function expects a Vehicle object or subclass. Car extends Vehicle, so passing Car is valid.
  2. Step 2: Analyze output of get_class()

    get_class($v) returns the actual class name of the object, which is 'Car'.
  3. Final Answer:

    Car -> Option C
  4. Quick Check:

    Type hint accepts subclass; get_class returns actual class [OK]
Quick Trick: get_class shows actual object class, not type hint [OK]
Common Mistakes:
  • Assuming output is parent class name
  • Expecting a type error for subclass
  • Thinking no output is produced

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes