Skip to content

Instantly share code, notes, and snippets.

@giorgiosironi
Created November 28, 2011 13:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giorgiosironi/1400405 to your computer and use it in GitHub Desktop.
Save giorgiosironi/1400405 to your computer and use it in GitHub Desktop.
With PHP 5.4, you can add setters to an object on the fly! [/sarcasm]
class ObjectEncapsulatingState
{
private $number;
public function __construct($number)
{
$this->number = $number;
}
}
$object = new ObjectEncapsulatingState(42);
$modifier = function () {
$this->number = 23;
};
// $modifier = $modifier->bindTo($object); // still avoids access to $this->number
$modifier = $modifier->bindTo($object, $object);
$modifier();
var_dump($object);
// output contains ["number":"ObjectEncapsulatingState":private]=> int(23)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment