Skip to content

Regression - PropertyAccess no longer works with classes + dynamically added properties #37615

Closed
@onehorsetown

Description

@onehorsetown

Symfony version(s) affected: x.y.z

Description
In version 5.0.10, one could use a PropertyAssessor to fetch properties that were dynamically added to a class instance. In version 5.1.0, this functionality was removed/broken.

How to reproduce

class Person  {
    public function __construct(string $name) {
        $this->name = $name;
    }
}

$accessor = PropertyAccess::createPropertyAccessor();

$person = new Person("Joe Consumer");

/* 
  The following works correctly in version 5.0.10.
  In versions 5.1.0 - 5.1.2 it throws an exception: "Can't get a way to read the property "name" in class..." 
*/
$name = $accessor->getValue($person, 'name');  

Possible Solution
One solution is to declare the property in the class definition, but this does not solve the issue for dynamically added properties.

Underlying regression is here:

} elseif ($object instanceof \stdClass && property_exists($object, $property)) {

The Person class in this example is not an instanceof \stdClass.

Additional context

Same issue reported when writing to a class instance: #37026

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