[php-src] Issue #13036: Abstract Constructor Property Promotion and Trait Properties causes PHP Fatal error
| From: | IwThPh | Date: | Wed, 27 Dec 2023 12:35:48 +0000 |
| Subject: | [php-src] Issue #13036: Abstract Constructor Property Promotion and Trait Properties causes PHP Fatal error | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-246132@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/13036
Author: IwThPh
### Description
The following code:
https://3v4l.org/eQOGs#v8.3.1
```php
<?php
trait PropertiesTrait
{
public ?array $x = [];
}
class AbstractPropertiesExample
{
public function __construct(
public ?array $x = []
) {}
}
class PropertiesExample extends AbstractPropertiesExample
{
use PropertiesTrait;
}
$example = new PropertiesExample;
$example->x;
```
Resulted in this output:
```
PHP Fatal error: AbstractPropertiesExample and PropertiesTrait define the same property ($x) in the
composition of PropertiesExample. However, the definition differs and is considered incompatible.
Class was composed in /dev/trait-property-promotion-test.php on line 15
Fatal error: AbstractPropertiesExample and PropertiesTrait define the same property ($x) in the
composition of PropertiesExample. However, the definition differs and is considered incompatible.
Class was composed in /dev/trait-property-promotion-test.php on line 15
```
But I expected this output instead:
```
```
The code conforms to the limitations outlined under Example 12 Defining Properties for traits. https://www.php.net/manual/en/language.oop5.traits.php#language.oop5.traits.properties
The following code without constructor property promotion, works as expected:
https://3v4l.org/0ZmXt#v8.3.1
```
<?php
trait PropertiesTrait
{
public array $x = [];
}
class AbstractPropertiesExample
{
public array $x = [];
public function __construct(
?array $x = []
) {
$this->x = $x;
}
}
class PropertiesExample extends AbstractPropertiesExample
{
use PropertiesTrait;
}
$example = new PropertiesExample;
$example->x;
```
Potentially related issue: #9272
### PHP Version
PHP 8.3.1
### Operating System
_No response_