Skip to content

Try bindTo instead of reflection #89

@samdark

Description

@samdark

The following could be used to write private / protected properties without reflection. Could be faster.

https://3v4l.org/GqaLf

<?php

final class Guard
{
    private $x = 'test';
}

$guard = new Guard();

$thief = function ($name)
{
    return $this->$name;
};

$hydrator = function ($name, $value) {
    $this->$name = $value;
};


echo $thief->bindTo($guard, Guard::class)('x') . "\n";

$hydrator->bindTo($guard, Guard::class)('x', 'bla');

echo $thief->bindTo($guard, Guard::class)('x') . "\n";

See https://ocramius.github.io/blog/accessing-private-php-class-members-without-reflection/

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