Skip to content

Instantly share code, notes, and snippets.

@bdelespierre
Created November 22, 2013 16:05
Show Gist options
  • Select an option

  • Save bdelespierre/7602300 to your computer and use it in GitHub Desktop.

Select an option

Save bdelespierre/7602300 to your computer and use it in GitHub Desktop.
<?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