title: injecting codes (methods) into class?
body: <p>Take this simple code:</p>
  
  <pre><code>class MyClass {
     public $customFunction = array();
  
     public function run($name){
         call_user_func($this-&gt;customFunction[$name]);
     }
  }
  
  //&gt; Usage:
  
  $c = new MyClass();
  $c-&gt;customFunction['first'] = function (){ /* some code*/ };
  
  $c-&gt;run('first');
  </code></pre>
  
  <p>This cose works as excpted. I add that function to <code>$customFunction</code> and then i can call it form <code>run();</code> method.</p>
  
  <p>The problem comes when in my injected function I try to do something object-related, for example if i add this function:</p>
  
  <pre><code>$c-&gt;customFunction['first'] = function(){ $this-&gt;callsomemethod(); }
  </code></pre>
  
  <p>When i call the <code>run();</code> method PHP says I can't use <code>$this</code> in a static context. </p>
  
  <p>Do I have some way to inject those function and be able to use the object method?</p>
  
  <p>(Note: of course my class is just an example, I need this for a scope)<br>
  Thanks</p>
  
  <h2>Thanks to Mario, here the solution I will use:</h2>
  
  <pre><code>public function run($name) {
      call_user_func($this-&gt;customFunction[$name],$this);
  }
  </code></pre>
  
  <p>At this point i just need to add function with a parm like this:</p>
  
  <pre><code>= function ($this) { /*some code*/};
  </code></pre>
  
  <p>And <code>$this</code> will emulate the object-context scope.</p>