note 127000 added to language.oop5.variance
| From: | jotaelesalinas at example dot com | Date: | Thu, 21 Apr 2022 15:55:36 +0000 |
| Subject: | note 127000 added to language.oop5.variance | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-218301@lists.php.net to get a copy of this message | ||
Bear in mind that, although the PHP syntax does not allow stricter method parameter types, you can
achieve it programmatically:
<?php
class AnimalFood { ... }
class CatFood extends AnimalFood { ... }
class Animal
{
public function eat(AnimalFood $food) {
echo $this->name . " eats " . get_class($food);
}
}
class Cat extends Animal
{
public function eat(AnimalFood $food) {
if (!$food instanceof CatFood) {
throw new \InvalidArgumentException(...);
}
parent::eat($food);
}
}
?>
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.135 (proxied: 91.205.194.1)
----
Manual Page -- https://php.net/manual/en/language.oop5.variance.php
Edit -- https://main.php.net/note/edit/127000
Del: integrated -- https://main.php.net/note/delete/127000/integrated
Del: useless -- https://main.php.net/note/delete/127000/useless
Del: bad code -- https://main.php.net/note/delete/127000/bad+code
Del: spam -- https://main.php.net/note/delete/127000/spam
Del: non-english -- https://main.php.net/note/delete/127000/non-english
Del: in docs -- https://main.php.net/note/delete/127000/in+docs
Del: other reasons-- https://main.php.net/note/delete/127000
Reject -- https://main.php.net/note/reject/127000
Search -- https://main.php.net/manage/user-notes.php