Skip to content

Instantly share code, notes, and snippets.

@bantya
Created August 25, 2017 13:00
Show Gist options
  • Save bantya/1707a42994a74d5cec3f99995e140afc to your computer and use it in GitHub Desktop.
Save bantya/1707a42994a74d5cec3f99995e140afc to your computer and use it in GitHub Desktop.
php: Accessing private php class members without reflection
<?php
# A generic property reader abstraction
# https://ocramius.github.io/blog/accessing-private-php-class-members-without-reflection/
$reader = function & ($object, $property) {
$value = & Closure::bind(function & () use ($property) {
return $this->$property;
}, $object, $object)->__invoke();
return $value;
};
$kitchen = new Kitchen();
$cake = & $reader($kitchen, 'cake');
$cake = 'sorry, I ate it!';
var_dump($kitchen);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment