note 125704 added to language.oop5.variance
| From: | phpnet-at-kennel17-dotco-dotuk@php.net | Date: | Wed, 20 Jan 2021 17:51:16 +0000 |
| Subject: | note 125704 added to language.oop5.variance | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-216209@lists.php.net to get a copy of this message | ||
Following the examples above, you might assume the following would be possible.
<?php
class CatFood extends AnimalFood { ... }
class Cat extends Animal
{
public function eat(CatFood $food) {
echo $this->name . " eats " . get_class($food);
}
}
?>
However, the Liskov Substitution Prinicpal, and therefore PHP, forbids this. There's no way
for cats to eat cat food, if animals are defined as eating animal food.
There are a large number of legitimate abstractions that are forbidden by PHP, due to this
restriction.
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.159.166 (proxied: 212.69.47.82)
----
Manual Page -- http://php.net/manual/en/language.oop5.variance.php
Edit -- https://master.php.net/note/edit/125704
Del: integrated -- https://master.php.net/note/delete/125704/integrated
Del: useless -- https://master.php.net/note/delete/125704/useless
Del: bad code -- https://master.php.net/note/delete/125704/bad+code
Del: spam -- https://master.php.net/note/delete/125704/spam
Del: non-english -- https://master.php.net/note/delete/125704/non-english
Del: in docs -- https://master.php.net/note/delete/125704/in+docs
Del: other reasons-- https://master.php.net/note/delete/125704
Reject -- https://master.php.net/note/reject/125704
Search -- https://master.php.net/manage/user-notes.php