Bird
0
0

Which of the following is the correct syntax to type hint a function parameter with a parent class Animal in PHP?

easy📝 Syntax Q12 of 15
PHP - Inheritance and Polymorphism

Which of the following is the correct syntax to type hint a function parameter with a parent class Animal in PHP?

function feed(____ $pet) { /* ... */ }
AAnimal $pet
B$pet Animal
Cclass Animal $pet
Dtype Animal $pet
Step-by-Step Solution
Solution:
  1. Step 1: Recall PHP type hint syntax

    In PHP, the type hint comes before the variable name: Type $variable.
  2. Step 2: Apply to the example

    For class Animal, the correct syntax is Animal $pet.
  3. Final Answer:

    Animal $pet -> Option A
  4. Quick Check:

    Type hint before variable name = Animal $pet [OK]
Quick Trick: Type hint is class name before variable with $ [OK]
Common Mistakes:
  • Placing variable before type hint
  • Adding extra keywords like 'class' or 'type'
  • Omitting the $ sign on variable

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes