Improve incompatible trait property error message
Open
Nobody has claimed this yet.
Feature
Status: Verified
- Dominant language
- C
- Stars
- 40.4k
- Forks
- 8.1k
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 96
Description
Description
The following code results in a PHP Fatal Error:
trait FooTrait
{
private array $options = [];
}
class Foo
{
use FooTrait;
public function __construct(private array $options = []) {}
}
The error thrown is
PHP Fatal error: Foo and FooTrait define the same property ($options) in the composition of Foo. However, the definition differs and is considered incompatible.
However, the same thing defined in the class instead of in the constructor works:
trait FooTrait
{
private array $options = [];
}
class Foo
{
use FooTrait;
private array $options = [];
}
As does the same thing, but without setting a default value:
trait FooTrait
{
private array $options;
}
class Foo
{
use FooTrait;
public function __construct(private array $options) {}
}
This seems to be ane rror
PHP Version
PHP 8.1.29 to PHP 8.4.0-dev
Operating System
MAC OSX 14.7
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No source file or test is identified in the issue. Reproduce the examples on the listed PHP versions, then trace trait property composition with constructor-promoted properties; done means the incompatible-property behavior is corrected and covered by a regression test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100