0.0187110900879
0.0127809047699
0.000743865966797
proxified = new \$name;
foreach(get_class_methods(\$name) as \$method) {
runkit_method_remove($proxy, \$method);
}
}
public function setLocator(ServiceLocator \$locator) {
\$this->locator = \$locator;
}
public function __call(\$m, \$a) {
\$this->locator->before(\$this->proxified, \$m);
\$result = call_user_func_array(array(\$this->proxified, \$m), \$a);
\$this->locator->after(\$this->proxified, \$m);
return \$result;
}
}");
$obj = new $proxy();
$obj->proxify($name);
$obj->setLocator($this);
return $obj;
}
public function get2($name) {
$proxy = $name."_Proxy2";
$str = "
class ".$name."_Proxy2 extends ".$name." {
private \$proxified;
private \$locator;
public function __construct(ServiceLocator \$locator) {
\$this->locator = \$locator;
}
public function proxify(\$obj) {
\$this->proxified = \$obj;
}
";
$class = new ReflectionClass($name);
foreach($class->getMethods() as $method) {
$str .= "
public function ".$method->getName()."() {
\$this->locator->before(\$this->proxified, \$m);
\$result = parent::".$method->getName()."();
\$this->locator->after(\$this->proxified, \$m);
return \$result;
}
";
}
$str .= "}";
eval($str);
$obj = new $proxy($this);
$obj->proxify($name);
return $obj;
}
public function before($obj, $method) {
$name = get_class($obj);
$method = 'before'.$method;
if(isset($this->listeners[$name]))
$this->listeners[$name]->$method($obj);
}
public function after($obj, $method) {
$name = get_class($obj);
$method = 'after'.$method;
if(isset($this->listeners[$name]))
$this->listeners[$name]->$method($obj);
}
public function setListener($name, EventListener $listener) {
$this->listeners[$name] = $listener;
}
}
class EventListener {
public function __call($m, $a) {
//print $m."
";
}
}
function getmicrotime() { list($usec, $sec) = explode(" ",microtime()); return ((float)$usec + (float)$sec); }
$l = new ServiceLocator;
$l->setListener('A', new EventListener());
$obj = $l->get("A");
$m = getmicrotime();
$n = 1000;
while($n--) {
$obj->foo();
}
print (getmicrotime() - $m)."
";
$obj = $l->get2("A");
$m = getmicrotime();
$n = 1000;
while($n--) {
$obj->foo();
}
print (getmicrotime() - $m)."
";
$m = getmicrotime();
$n = 1000;
$obj = new A;
while($n--) {
$obj->foo();
}
print (getmicrotime() - $m)."
";
show_source(__FILE__);
?>