Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try bindTo instead of reflection #89

Open
samdark opened this issue Jul 25, 2024 · 1 comment
Open

Try bindTo instead of reflection #89

samdark opened this issue Jul 25, 2024 · 1 comment

Comments

@samdark
Copy link
Member

samdark commented Jul 25, 2024

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/

@samdark
Copy link
Member Author

samdark commented Jul 25, 2024

Should be benchmarked with current PHP versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant