Bird
Raised Fist0
PHPprogramming~20 mins

Type hinting with parent classes in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
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
intermediate
2: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);
?>
ABark
BAnimal sound
CFatal error: Argument must be of type Animal
DEmpty output
Attempts:
2 left
💡 Hint
Remember that type hinting with a parent class allows instances of child classes.
❓ Predict Output
intermediate
2: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();
?>
AFatal error: Cannot return subclass instance
BAnimal sound
CMeow
DTypeError: Return value must be of type Animal
Attempts:
2 left
💡 Hint
Returning a subclass instance is allowed when the return type is a parent class.
🔧 Debug
advanced
2: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());
?>
AOutputs: Handling dog
BFatal error: Argument must be of type Dog, Bird given
CNo output, silently ignored
DParse error: Unexpected token
Attempts:
2 left
💡 Hint
Check if Bird is a Dog or subclass of Dog.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error with type hinting?
Which of the following PHP function declarations will cause a syntax error?
Afunction foo(Animal|Dog $a) {}
Bfunction foo(Animal $a): Dog {}
Cfunction foo(Animal $a): Dog|Animal {}
Dfunction foo(Animal $a): Animal|Dog {}
Attempts:
2 left
💡 Hint
Union types in parameters must be valid classes or types; check if Animal|Dog is allowed.
🚀 Application
expert
3: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);
}
?>
A4
B1
C2
D3
Attempts:
2 left
💡 Hint
Only instances of Animal or its subclasses are accepted.

Practice

(1/5)
1.

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

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

easy
A. It disables type checking for the function parameter.
B. It restricts the function to accept only the exact parent class, not child classes.
C. It only accepts objects of child classes, not the parent class itself.
D. It allows passing any object that is an instance of the parent class or its child classes.

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]
Hint: 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
2.

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

function feed(____ $pet) { /* ... */ }
easy
A. Animal $pet
B. $pet Animal
C. class Animal $pet
D. type Animal $pet

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]
Hint: 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
3.

Given the code below, what will be the output?

class Vehicle {}
class Car extends Vehicle {}

function startEngine(Vehicle $v) {
    echo get_class($v);
}

$car = new Car();
startEngine($car);
medium
A. Error: Type mismatch
B. Vehicle
C. Car
D. No output

Solution

  1. Step 1: Understand type hinting with inheritance

    The function expects a Vehicle object or subclass. Car extends Vehicle, so passing Car is valid.
  2. Step 2: Analyze output of get_class()

    get_class($v) returns the actual class name of the object, which is 'Car'.
  3. Final Answer:

    Car -> Option C
  4. Quick Check:

    Type hint accepts subclass; get_class returns actual class [OK]
Hint: get_class shows actual object class, not type hint [OK]
Common Mistakes:
  • Assuming output is parent class name
  • Expecting a type error for subclass
  • Thinking no output is produced
4.

What is the error in the following PHP code?

class Fruit {}
class Apple extends Fruit {}

function eat(Apple $fruit) {
    echo "Eating apple";
}

$fruit = new Fruit();
eat($fruit);
medium
A. Syntax error: missing semicolon
B. Fatal error: Argument must be of type Apple, Fruit given
C. No error, outputs 'Eating apple'
D. Warning: Undefined variable $fruit

Solution

  1. Step 1: Check type hint and argument

    The function expects an Apple object, but the argument is a Fruit object (parent class).
  2. Step 2: Understand PHP type hint rules

    PHP requires the argument to be the hinted class or subclass, not a parent class. Passing Fruit causes a fatal error.
  3. Final Answer:

    Fatal error: Argument must be of type Apple, Fruit given -> Option B
  4. Quick Check:

    Type hint mismatch with parent class argument = fatal error [OK]
Hint: Child class hint rejects parent class objects [OK]
Common Mistakes:
  • Thinking parent class can replace child hint
  • Expecting no error on type mismatch
  • Confusing syntax error with type error
5.

Consider the following classes and function:

class Employee {}
class Manager extends Employee {}
class Developer extends Employee {}

function assignTask(Employee $e) {
    if ($e instanceof Manager) {
        return "Assigning management task";
    } elseif ($e instanceof Developer) {
        return "Assigning coding task";
    } else {
        return "Assigning general task";
    }
}

$worker = new Manager();
echo assignTask($worker);

What will be the output and why?

hard
A. Assigning management task
B. Assigning coding task
C. Assigning general task
D. Type error: argument must be Employee

Solution

  1. Step 1: Check type hint and argument

    The function accepts any Employee or subclass. Manager is a subclass, so passing $worker is valid.
  2. Step 2: Evaluate instanceof conditions

    The code checks if $e is a Manager, which is true, so it returns "Assigning management task".
  3. Final Answer:

    Assigning management task -> Option A
  4. Quick Check:

    Type hint accepts subclass; instanceof detects Manager [OK]
Hint: Type hint parent + instanceof child checks for behavior [OK]
Common Mistakes:
  • Expecting type error for subclass argument
  • Ignoring instanceof checks order
  • Assuming general task is assigned
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard