[php-src] Issue #9915: Polymorphism not working on abstract method
| From: | roke22 | Date: | Wed, 09 Nov 2022 18:55:55 +0000 |
| Subject: | [php-src] Issue #9915: Polymorphism not working on abstract method | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-242776@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/9915
Author: roke22
### Description
Hello,
This is working fine and is how to use the polymorphism
```php
<?php
abstract class Class1
{
public function __construct(BaseEntity $entity)
{
echo "Constructor".PHP_EOL;
}
}
class MyClass1 extends Class1
{
}
class BaseEntity
{
}
class Entity extends BaseEntity
{
}
$class1 = new MyClass1((new Entity));
```
Resulted in this output:
```
Constructor
```
But when you do the same with an abstract public function like this example:
```php
<?php
abstract class Class2
{
abstract public function test(BaseEntity $entity);
}
class MyClass2 extends Class2
{
public function test(Entity $entity)
{
echo "Test".PHP_EOL;
}
}
class BaseEntity
{
}
class Entity extends BaseEntity
{
}
$class2 = new MyClass2();
$class2->test((new Entity));
```
You get the error
```
PHP Fatal error: Declaration of MyClass2::test(Entity $entity) must be compatible with
Class2::test(BaseEntity $entity)
```
Whe that should work as expected becuse the $entity parameter is a BaseEntity too.
### PHP Version
PHP 7.4.3 (cli) (built: Nov 2 2022 09:53:44) ( NTS )
### Operating System
Ubuntu 20.04