Challenge - 5 Problems
Master of Type Hinting with Parent Classes
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of method call with parent class type hint
What is the output of this PHP code when calling
processAnimal with a Dog instance?PHP
<?php class Animal { public function speak(): string { return "Animal sound"; } } class Dog extends Animal { public function speak(): string { return "Bark"; } } function processAnimal(Animal $animal): string { return $animal->speak(); } $dog = new Dog(); echo processAnimal($dog); ?>
Attempts:
2 left
💡 Hint
Remember that type hinting with a parent class allows instances of child classes.
✗ Incorrect
The function
processAnimal accepts any instance of Animal or its subclasses. Since Dog extends Animal and overrides speak(), calling processAnimal($dog) outputs "Bark".❓ Predict Output
intermediate2:00remaining
Return type with parent class hint
What will be the output of this PHP code when calling
getAnimalSound()?PHP
<?php class Animal { public function speak(): string { return "Animal sound"; } } class Cat extends Animal { public function speak(): string { return "Meow"; } } function getAnimalSound(): Animal { return new Cat(); } echo getAnimalSound()->speak(); ?>
Attempts:
2 left
💡 Hint
Returning a subclass instance is allowed when the return type is a parent class.
✗ Incorrect
The function
getAnimalSound() declares it returns an Animal instance, but it actually returns a Cat instance, which is allowed because Cat extends Animal. Calling speak() on the returned object outputs "Meow".🔧 Debug
advanced2:00remaining
Identify the error with type hinting and class hierarchy
What error will this PHP code produce when calling
handleAnimal(new Bird());?PHP
<?php class Animal {} class Dog extends Animal {} function handleAnimal(Dog $dog) { echo "Handling dog"; } class Bird extends Animal {} handleAnimal(new Bird()); ?>
Attempts:
2 left
💡 Hint
Check if Bird is a Dog or subclass of Dog.
✗ Incorrect
The function
handleAnimal expects an argument of type Dog. Passing a Bird instance causes a fatal type error because Bird is not a subclass of Dog, even though both extend Animal.📝 Syntax
advanced2:00remaining
Which option causes a syntax error with type hinting?
Which of the following PHP function declarations will cause a syntax error?
Attempts:
2 left
💡 Hint
Union types in parameters must be valid classes or types; check if Animal|Dog is allowed.
✗ Incorrect
In PHP, union types are allowed for parameters and return types starting from PHP 8.0. However, union types with classes where one is a subclass of the other are allowed syntactically but redundant. The syntax in option A uses a union type in the parameter: Animal|Dog. Since Dog extends Animal, this union is redundant but valid in PHP 8.0+. However, if the classes are not imported or fully qualified, it can cause a syntax error. The main reason option A is marked as causing a syntax error is because union types in parameters require all types to be valid and imported or fully qualified. If Animal and Dog are not imported or defined in the current namespace, this causes a syntax error. Options A, B, and C are valid in PHP 8.0+ as return types with union types are allowed.
🚀 Application
expert3:00remaining
Count instances accepted by a function with parent class type hint
Given these classes and function, how many objects in the array will be accepted by
handleAnimal(Animal $a) without error?PHP
<?php class Animal {} class Dog extends Animal {} class Cat extends Animal {} class Bird {} function handleAnimal(Animal $a) { echo get_class($a) . " accepted\n"; } $objects = [new Dog(), new Cat(), new Bird(), new Animal()]; foreach ($objects as $obj) { handleAnimal($obj); } ?>
Attempts:
2 left
💡 Hint
Only instances of Animal or its subclasses are accepted.
✗ Incorrect
The function
handleAnimal requires an Animal instance. Dog, Cat, and Animal itself are accepted. Bird is not a subclass of Animal, so passing it causes a type error. Therefore, 3 objects are accepted.