Skip to content

False error when property incompatibility in a class and a trait if the property is defined in the constructor. #16525

@EdmondDantes

Description

@EdmondDantes

Description

The following code:

trait someTrait
{
    protected array $attributes = [];
}

class test
{
    use someTrait;
    
    public function __construct(
        protected array $attributes = []
    ) {}
}

$test = new test([1,2,3]);

Resulted in this output:

Fatal error: test and someTrait define the same property ($attributes) in the composition of test. However, the definition differs and is considered incompatible. Class was composed in test.php on line 11

But I expected this output instead:

No errors

P.S. If the property is defined outside the constructor, everything works fine!

PHP Version

PHP 8.3.4

Operating System

Windows 11

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