All pastes #109039 Raw Edit

Anonymous

public php v1 · immutable
#109039 ·published 2006-07-31 20:17 UTC
rendered paste body
0.01871109008790.01278090476990.000743865966797<?php class A {      function foo() {          //return "foo!\n";      }  }  class B {      function foo() {          //return "foo!\n";      }  }     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 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."<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"); $m = getmicrotime(); $n = 1000; while($n--) {     $obj->foo(); } print (getmicrotime() - $m)."<br \>"; $obj = $l->get2("A");   $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__); ?>