Bird
0
0

Given the classes below, which function call will NOT cause a type error?

hard📝 Application Q8 of 15
PHP - Inheritance and Polymorphism
Given the classes below, which function call will NOT cause a type error?
class Device {}
class Phone extends Device {}
class Tablet extends Device {}

function useDevice(Device $d) {
    echo "Using device";
}

Choose the correct call:
AuseDevice('Tablet');
BuseDevice(new stdClass());
CuseDevice('Device');
DuseDevice(new Phone());
Step-by-Step Solution
Solution:
  1. Step 1: Understand type hinting with parent class Device

    The function accepts Device objects or any subclass instances.
  2. Step 2: Check each call

    A and C pass strings (invalid), B passes stdClass (unrelated class, invalid), D passes new Phone() which extends Device (valid).
  3. Final Answer:

    useDevice(new Phone()); -> Option D
  4. Quick Check:

    Parent class hint accepts parent and subclass objects [OK]
Quick Trick: Parent class hint accepts all subclasses, rejects unrelated types [OK]
Common Mistakes:
  • Passing unrelated objects like stdClass
  • Passing strings instead of objects
  • Assuming only parent class instances allowed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes