Bird
0
0

What does type hinting with a parent class in PHP allow?For example, using function foo(ParentClass $obj).

easy📝 Conceptual Q11 of 15
PHP - Inheritance and Polymorphism

What does type hinting with a parent class in PHP allow?

For example, using function foo(ParentClass $obj).

AIt disables type checking for the function parameter.
BIt restricts the function to accept only the exact parent class, not child classes.
CIt only accepts objects of child classes, not the parent class itself.
DIt allows passing any object that is an instance of the parent class or its child classes.
Step-by-Step Solution
Solution:
  1. Step 1: Understand type hinting with parent classes

    Type hinting with a parent class means the function accepts objects of that class or any subclass.
  2. Step 2: Apply to the example

    Using ParentClass $obj allows passing instances of ParentClass or any class that extends it.
  3. Final Answer:

    It allows passing any object that is an instance of the parent class or its child classes. -> Option D
  4. Quick Check:

    Parent class type hint = accepts parent or child objects [OK]
Quick Trick: Parent class hint accepts parent and child objects [OK]
Common Mistakes:
  • Thinking only exact parent class objects are accepted
  • Believing child classes are excluded
  • Confusing type hinting with disabling checks

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes