Doc #79538 [NEW]: Incorrect type hint in language.oop5.variance
| From: | david at codepoets dot co dot uk | Date: | Wed, 29 Apr 2020 12:12:24 +0000 |
| Subject: | Doc #79538 [NEW]: Incorrect type hint in language.oop5.variance | ||
| Groups: | php.doc.bugs | ||
| Request: | Send a blank email to doc-bugs+get-17531@lists.php.net to get a copy of this message | ||
From: david at codepoets dot co dot uk
Operating system: n/a
PHP version: Irrelevant
Package: Documentation problem
Bug Type: Documentation Problem
Bug description:Incorrect type hint in language.oop5.variance
Description:
------------
---
From manual page: https://php.net/language.oop5.variance
---
in the Contravariance section there is :
abstract class Animal
{
protected string $name;
public function __construct(string $name)
{
$this->name = $name;
}
public function eat(AnimalFood $food)
{
echo $this->name . " eats " . get_class($food);
}
}
The eat method should instead take Food as a type hint, and not
AnimalFood.
i.e. something more like :
abstract class Animal
{
.....
public function eat(Food $food)
{
echo $this->name . " eats " . get_class($food);
}
}
Test script:
---------------
<?php
abstract class Animal
{
protected $name;
public function __construct(string $name)
{
$this->name = $name;
}
abstract public function speak();
public function eat(AnimalFood $food)
{
echo $this->name . " eats " . get_class($food);
}
}
class Dog extends Animal
{
public function speak()
{
echo $this->name . " barks";
}
}
class Cat extends Animal
{
public function speak()
{
echo $this->name . " meows";
}
}
interface AnimalShelter
{
public function adopt(string $name): Animal;
}
class CatShelter implements AnimalShelter
{
public function adopt(string $name): Cat // instead of returning
class type Animal, it can return class type Cat
{
return new Cat($name);
}
}
class DogShelter implements AnimalShelter
{
public function adopt(string $name): Dog // instead of returning
class type Animal, it can return class type Dog
{
return new Dog($name);
}
}
class Food {}
class AnimalFood extends Food {}
$kitty = (new CatShelter)->adopt("Ricky");
$catFood = new AnimalFood();
$kitty->eat($catFood);
echo "\n";
$doggy = (new DogShelter)->adopt("Mavrick");
$banana = new Food();
$doggy->eat($banana);
Expected result:
----------------
Ricky eats AnimalFood
Mavrick eats Food
Actual result:
--------------
david@walnut:~/src/php-library/animals$ php7.4 animals.php
Ricky eats AnimalFood
PHP Fatal error: Uncaught TypeError: Argument 1 passed to Animal::eat()
must be an instance of AnimalFood, instance of Food given, called in
/home/david/src/php-library/animals/animals.php on line 75 and defined
in /home/david/src/php-library/animals/animals.php:15
Stack trace:
#0 /home/david/src/php-library/animals/animals.php(75): Animal->eat()
#1 {main}
thrown in /home/david/src/php-library/animals/animals.php on line 15
--
Edit bug report at https://bugs.php.net/bug.php?id=79538&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=79538&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=79538&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=79538&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=79538&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=79538&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=79538&r=support
Expected behavior: https://bugs.php.net/fix.php?id=79538&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=79538&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=79538&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=79538&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=79538&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=79538&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=79538&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=79538&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=79538&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=79538&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=79538&r=mysqlcfg