0
0
PHPprogramming~10 mins

Type hinting with parent classes in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Type hinting with parent classes
Define Parent Class
Define Child Class extends Parent
Function with parameter type hinted as Parent
Call function with Parent instance
Accept
Call function with Child instance
Accept
Call function with unrelated class
Error
The flow shows how a function expects a parameter of the parent class type, allowing instances of the parent or any child class to be passed safely.
Execution Sample
PHP
<?php
class Animal {}
class Dog extends Animal {}
function greet(Animal $a) {
  echo "Hello!";
}
greet(new Dog());
?>
This code defines a parent class Animal and a child class Dog. The greet function accepts an Animal type, so it also accepts Dog instances.
Execution Table
StepCode LineActionParameter TypeResult
1class Animal {}Define parent class Animal-Animal class created
2class Dog extends Animal {}Define child class Dog-Dog class created, inherits Animal
3function greet(Animal $a) {...}Define function greet with Animal type hintAnimalFunction greet ready
4greet(new Dog())Call greet with Dog instanceDog (child of Animal)Accepted, prints 'Hello!'
5greet(new Animal())Call greet with Animal instanceAnimalAccepted, prints 'Hello!'
6greet(new stdClass())Call greet with unrelated classstdClassError: Type hint violation
💡 Execution stops on error when passing an unrelated class instance to greet.
Variable Tracker
VariableStartAfter greet(new Dog())After greet(new Animal())After greet(new stdClass())
$aundefinedDog instanceAnimal instancestdClass instance (error)
Key Moments - 2 Insights
Why can the greet function accept a Dog instance when it expects an Animal?
Because Dog is a child class of Animal, it inherits from Animal, so it matches the type hint. See execution_table row 4.
What happens if we pass an unrelated class instance to greet?
It causes a type hint error because the instance does not inherit from Animal. See execution_table row 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what type is accepted by the greet function parameter $a?
AAny class instance
BOnly instances of Animal
CInstances of Animal and its child classes
DOnly instances of Dog
💡 Hint
Check rows 4 and 5 where greet accepts Dog and Animal instances.
At which step does the program produce a type hint error?
AStep 6
BStep 4
CStep 5
DNo error occurs
💡 Hint
See execution_table row 6 for the error with stdClass instance.
If we remove 'extends Animal' from class Dog, what happens when calling greet(new Dog())?
AIt works fine
BIt causes a type hint error
CDog becomes a parent class
DNo change in behavior
💡 Hint
Without inheritance, Dog is unrelated to Animal, so greet(new Dog()) fails type hint.
Concept Snapshot
Type hinting with parent classes in PHP:
- Use class ParentClass {}
- Child classes extend ParentClass
- Function parameters can be type hinted as ParentClass
- Accepts instances of ParentClass or any child class
- Passing unrelated classes causes type errors
Full Transcript
This example shows how PHP type hinting works with parent classes. We define a parent class Animal and a child class Dog that extends Animal. The function greet expects a parameter of type Animal. Because Dog inherits from Animal, passing a Dog instance to greet is allowed. Passing an unrelated class instance causes a type hint error. This helps ensure functions get the right kind of objects, making code safer and clearer.