Created
November 22, 2013 16:05
-
-
Save bdelespierre/7602300 to your computer and use it in GitHub Desktop.
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
| <?php | |
| // how to reverse the singleton pattern | |
| class Service { | |
| public static $data; | |
| public static function inc () { | |
| static::$data++; | |
| } | |
| } | |
| class Proxy { | |
| public function __construct ($service_class) { | |
| $this->_serviceClass = $service_class; | |
| } | |
| public function __call ($method, $args) { | |
| if (!method_exists($this->_serviceClass, $method)) | |
| throw new BadMethodCallException; | |
| return call_user_func_array(array($this->_serviceClass, $method), $args); | |
| } | |
| } | |
| $proxy_1 = new Proxy('Service'); | |
| $proxy_2 = new Proxy('Service'); | |
| $proxy_1->inc(); | |
| $proxy_2->inc(); | |
| $proxy_1->inc(); | |
| $proxy_2->inc(); | |
| var_dump( Service::$data ); // 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment