Created
November 28, 2011 13:31
-
-
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]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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