Skip to content

Improve incompatible trait property error message #16643

@bshaffer

Description

@bshaffer

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions