php / php/php-src

Improve incompatible trait property error message

Open
#16,643 2 comments 0 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.