Hacker News new | comments | show | ask | jobs | submit login

I am aware of the gnarliness of the following BUT You could do something like:

   class Object { 
       public function __call($method, $args) {
           if(isset($this->$method) && $this->$method instanceof \Closure) {
               $this->$method->bindTo($this);
               return call_user_func_array($this->$method, $args);
           }
       }
   }

   $context = new Object;
   $context->foo = "Hello World";
   $context->closure = function() {
       echo $this->foo;
   };
   $context->closure();
Once traits are in the language (5.5?) that could just be mixed in.



Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact

Search: