Skip to main content

rekalogika/direct-property-access

Implementation of Symfony's PropertyAccessorInterface that reads and writes directly to the object's properties, bypassing getters and setters.

Synopsis​

use Rekalogika\DirectPropertyAccess\DirectPropertyAccessor;

class Person
{
private string $name = 'Jane';
}

$propertyAccessor = new DirectPropertyAccessor();

$name = $propertyAccessor->getValue($person, 'name'); // Jane
$propertyAccessor->setValue($person, 'name', 'John');

Installation​

Make sure Composer is installed globally, as explained in the installation chapter of the Composer documentation.

Open a command console, enter your project directory, and execute:

composer require rekalogika/direct-property-access

Usage​

In Symfony projects, you can autowire DirectPropertyAccessor. In other projects, you can simply instantiate it.

Read Symfony's PropertyAccess documentation for more information on how to use it. The difference is that DirectPropertyAccessor does not call any of the object's methods, but reads and writes directly to the object's properties, even if they are private.

Caveats​

Currently does not support arrays and paths beyond one level deep.

Credits​

This project took inspiration from the following projects.

License​

MIT

Contributing​

Issues and pull requests should be filed in the GitHub repository rekalogika/direct-property-access.