Variance
An override in PHP may change a signature in two directions and must keep it in a third. Return types may narrow, parameter types may widen, and property types may not move at all.
Covariant returns
A child method may narrow the return type to a subtype. Callers that expect the parent type keep working, since the subtype fits wherever the parent fits.
Factories and finders use this shape most. The parent promises a broad type, and each child promises a sharper one.
<?php class Animal { } class Cat extends Animal { } class Shelter { public function get(): Animal { return new Animal(); } } class CatShelter extends Shelter { public function get(): Cat { return new Cat(); } } $plain = new Shelter(); $cats = new CatShelter(); echo get_class($plain->get()), PHP_EOL; echo get_class($cats->get()), PHP_EOL;
php return.php
Animal
Cat
The child returned the narrower type and the parent kept the broad one, and PHP accepted both signatures in one class tree.
Contravariant params
A child method may widen a parameter type to a supertype. Callers that pass the narrow type keep working, since the wide type accepts it.
Think of it as accepting more, not less. The child stays usable everywhere the parent was usable.
<?php class Animal { } class Cat extends Animal { } class Shelter { public function take(Cat $c): void { echo "narrow: ", get_class($c), PHP_EOL; } } class WideShelter extends Shelter { public function take(Animal $c): void { echo "wide: ", get_class($c), PHP_EOL; } } $wide = new WideShelter(); $wide->take(new Cat()); $wide->take(new Animal());
php params.php
wide: Cat
wide: Animal
The child takes the narrow type its parent promised and the wider one as well, so it is usable in every place the parent was.
No property variance
Properties must keep the exact same type in the child. Narrowing and widening are both blocked for property types.
Reads and writes pull in opposite directions, so any change would break one side. PHP asks for an exact match instead.
<?php class Animal { } class Cat extends Animal { } class Shelter { public Animal $pet; } class CatShelter extends Shelter { // public Cat $pet; would be refused: // Type of CatShelter::$pet must be Animal (as in class Shelter) public Animal $pet; } $s = new CatShelter(); $s->pet = new Cat(); echo get_class($s->pet), PHP_EOL;
php property.php
Cat
The child repeats the parent type, so the class loads, and the property still accepts the subtype at run time. The comment names the fatal error that a narrower declaration would raise.
How PHP checks
PHP checks signatures when the child class loads. A bad override stops the program with a fatal error right away.
Run the file once after changing any signature. Fast feedback beats long hunts through child classes.
<?php class Animal { } class Cat extends Animal { } class Shelter { public function get(): Animal { return new Animal(); } } class CatShelter extends Shelter { public function get(): Cat { return new Cat(); } } function adopt(Shelter $s): Animal { return $s->get(); } echo get_class(adopt(new Shelter())), PHP_EOL; echo get_class(adopt(new CatShelter())), PHP_EOL;
php check.php
Animal
Cat
The free function accepts the parent type, so it takes both objects, and the sharper return of the child comes through unchanged.
Exercises
- Add a Dog class and a DogShelter with a covariant return.
- Widen a parameter in a child class and call it with both types.
- Narrow a property type in a child and read the fatal error it produces.
- Pass a child object to a function typed with the parent and print the class you get back.
- Explain why a narrower return type cannot break a caller but a narrower parameter can.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What may a child method do with its return type?
Answer
Narrow it to a subtype
A narrower return still fits every caller that expected the parent type, so PHP allows it.
-
What may a child method do with a parameter type?
Answer
Widen it to a supertype
A wider parameter accepts everything the parent accepted, which keeps the child usable.
-
What happens to a property type in a child class?
Answer
It must match the parent type exactly
Properties are read and written, so no single direction of change is safe and PHP demands an exact type.
-
When does PHP report a bad override?
Answer
When the child class loads
The signature check happens at class declaration, so the program stops before any call.
-
What does this program print?
<?php class Animal { } class Cat extends Animal { } class Shelter { public function get(): Animal { return new Animal(); } } class CatShelter extends Shelter { public function get(): Cat { return new Cat(); } } $cats = new CatShelter(); echo get_class($cats->get());Answer
Cat
The child returns a Cat, and get_class() names the object that came back.
-
What does this program print?
<?php class Animal { } class Cat extends Animal { } class Shelter { public function take(Cat $c): void { echo "narrow: ", get_class($c); } } class WideShelter extends Shelter { public function take(Animal $c): void { echo "wide: ", get_class($c); } } $wide = new WideShelter(); $wide->take(new Cat());Answer
wide: Cat
The child widened the parameter and the object is a Cat, so the wide method runs and prints its own label.
-
What does this program print?
<?php class Animal { } class Cat extends Animal { } class Shelter { public Animal $pet; } class CatShelter extends Shelter { public Animal $pet; } $s = new CatShelter(); $s->pet = new Cat(); echo get_class($s->pet);Answer
Cat
The child repeats the exact type, so the class loads, and the property holds the Cat that was stored.
-
What does this program print?
<?php class Animal { } class Cat extends Animal { } class Shelter { public function get(): Animal { return new Animal(); } } class CatShelter extends Shelter { public function get(): Cat { return new Cat(); } } function adopt(Shelter $s): Animal { return $s->get(); } echo get_class(adopt(new CatShelter()));Answer
Cat
The function is typed with the parent and the object is the child, which returns a Cat.
-
Why can a narrower return type never break a caller?
Answer
Because the subtype also satisfies the parent type
Every operation allowed on the parent type is allowed on the subtype, so the caller sees nothing new.
-
Which message does PHP print for a narrowed property type in a child class?
Answer
Type of CatShelter::$pet must be Animal (as in class Shelter)
The message names the child property and the exact type the parent declared.
The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.
Article author: Arthur Isaev