All pastes #108320 Raw Edit

Stuff

public php v1 · immutable
#108320 ·published 2006-07-31 12:45 UTC
rendered paste body
object(A_Proxy)#3 (2) { ["proxified:private"]=> object(A)#4 (0) { } ["locator:private"]=> object(ServiceLocator)#1 (1) { ["listeners"]=> array(1) { ["A"]=> object(EventListener)#2 (0) { } } } } array(3) { [0]=> string(7) "proxify" [1]=> string(10) "setLocator" [2]=> string(6) "__call" } 0.06154704093930.00180101394653<?php class A {     function foo() {         //return "foo!\n";     }     function bar() {         return "bar!\n";     } } class B extends A { } class ServiceLocator {     public function get($name) {         $proxy = $name."_Proxy";         eval("             class ".$name."_Proxy extends $name {                                  private \$proxified;                 private \$locator;                 public function proxify(\$name) {                     \$this->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 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."<br \>";     } } function getmicrotime() { list($usec, $sec) = explode(" ",microtime()); return ((float)$usec + (float)$sec); }  $l = new ServiceLocator; $l->setListener('A', new EventListener()); $obj = $l->get("A"); var_dump($obj); var_dump(get_class_methods($obj)); $m = getmicrotime(); $n = 1000; while($n--) {     $obj->foo(); } print (getmicrotime() - $m)."<br \>"; $m = getmicrotime(); $n = 1000; $obj = new A; while($n--) {     $obj->foo(); } print (getmicrotime() - $m)."<br \>"; show_source(__FILE__); ?>