[php-src] Issue #15424: Properties (hooks) and inheritance
| From: | SerafimArts | Date: | Thu, 15 Aug 2024 12:41:07 +0000 |
| Subject: | [php-src] Issue #15424: Properties (hooks) and inheritance | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-247502@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/15424
Author: SerafimArts
### Description
Referenced RFC https://github.com/php/php-src/pull/13455
The following code:
```php
<?php
interface ExampleInterface {} // parent
interface MutableExampleInterface extends ExampleInterface {} // child
final class Example implements MutableExampleInterface {} // impl
// parent provider
interface ExampleProviderInterface
{
// Any type that implements ExampleInterface can be read
public ExampleInterface $property { get; }
}
// child provider that "overrides" parent definition
interface MutableExampleProviderInterface extends ExampleProviderInterface
{
// Any type that implements (MutableExampleInterface & ExampleInterface) can be read + write
// We supplement the object with the installation method
public MutableExampleInterface $property { get; set; }
}
// An implementation that uses implementation
final class ExampleProvider implements MutableExampleProviderInterface
{
public Example $property { // << implementation
// We can return the implementation, because it implements (MutableExampleInterface &
ExampleInterface)
get => $this->property;
// Any instance of ExampleInterface is allowed for set, since it satisfies the covariance
condition
set (ExampleInterface $v) => $this->property = new ExampleImplementation($v);
}
}
```
Resulted in this output:
```
Fatal error: Type of ExampleProvider::$property must be MutableExampleInterface (as in class
MutableExampleProviderInterface)
```
But I expected this output instead:
```
*nothing*
```
Same code (logic) using getters and setters instead of properties: https://onlinephp.io/c/4e180
```php
interface ExampleInterface {}
interface MutableExampleInterface extends ExampleInterface {}
final class Example implements MutableExampleInterface {}
interface ExampleProviderInterface
{
public function getProperty(): ExampleInterface;
}
interface MutableExampleProviderInterface extends ExampleProviderInterface
{
public function getProperty(): MutableExampleInterface;
public function setProperty(MutableExampleInterface $value): void;
}
final class ExampleProvider implements MutableExampleProviderInterface
{
public function getProperty(): Example {}
public function setProperty(ExampleInterface $value): void {}
}
```
### PHP Version
PHP 8.4-beta1
### Operating System
_No response_