Bird
0
0

Which of the following is the correct way to declare a function that accepts an object of class Vehicle or its subclasses in PHP?

easy📝 Syntax Q3 of 15
PHP - Inheritance and Polymorphism
Which of the following is the correct way to declare a function that accepts an object of class Vehicle or its subclasses in PHP?
Afunction start(Vehicle& $car) {}
Bfunction start($car: Vehicle) {}
Cfunction start(Vehicle $car) {}
Dfunction start(Vehicle? $car) {}
Step-by-Step Solution
Solution:
  1. Step 1: Understand PHP function parameter type hinting

    PHP requires the class name followed by a space and the parameter name with a $ sign.
  2. Step 2: Validate each option

    function start(Vehicle $car) {} matches correct syntax. Options A, B, and D use invalid or incorrect syntax for type hinting.
  3. Final Answer:

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

    Correct PHP type hint syntax = ClassName $param [OK]
Quick Trick: Type hint with ClassName $param, no colon or ampersand [OK]
Common Mistakes:
  • Using colon instead of space
  • Adding ampersand incorrectly
  • Misusing nullable syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes