Bird
0
0

Which of the following PHP function declarations correctly type hints a parameter to accept an object of the parent class Vehicle?

easy📝 Conceptual Q2 of 15
PHP - Inheritance and Polymorphism

Which of the following PHP function declarations correctly type hints a parameter to accept an object of the parent class Vehicle?

Afunction start(Vehicle v) {}
Bfunction start($v: Vehicle) {}
Cfunction start(Vehicle $v) {}
Dfunction start(&Vehicle $v) {}
Step-by-Step Solution
Solution:
  1. Step 1: Review PHP type hint syntax

    Type hinting is done by specifying the class name before the parameter with a $ sign.
  2. Step 2: Analyze options

    function start(Vehicle $v) {} uses correct syntax: Vehicle $v. function start($v: Vehicle) {} uses invalid syntax. function start(Vehicle v) {} misses the $ sign. function start(&Vehicle $v) {} uses a reference which is allowed but not related to type hinting correctness.
  3. Final Answer:

    function start(Vehicle $v) {} -> Option C
  4. Quick Check:

    Correct syntax: ClassName $variable [OK]
Quick Trick: Use ClassName $param for type hinting [OK]
Common Mistakes:
  • Omitting the $ sign before parameter name
  • Using colon syntax incorrectly
  • Confusing references with type hints

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes