title: Use variables inside an anonymous function, which is defined somewhere else
body: <p>When using anonymous functions in PHP, you can easily use variables from right outside of its scope by using the <code>use()</code> keyword.</p>
  
  <p>In my case the anonymous functions are already defined somewhere, but called later on (somewhere else) in a class.</p>
  
  <p>The following piece of code is to illustrate the idea:</p>
  
  <pre><code>&lt;?php
  
  $bla = function ( $var1 ) use ($arg)
          {
              echo $var1;
          };
  
  class MyClass
  {
      private $func;
  
      public function __construct ( $func )
      {
          $this-&gt;func = $func;
      }
  
      public function test ( $arg )
      {
          $closure =  $this-&gt;func;
          $closure ( 'anon func' );
      }
  }
  
  $c = new MyClass($bla);
  $c-&gt;test ( 'anon func' );
  </code></pre>
  
  <p>What i'm doing is i create an <code>anonymous function</code> and store that in a variable. I pass that variable to the method of a class and that is where i want to run the <em>anonymous</em> function.</p>
  
  <p>But i can't use the <code>use()</code> keyword to get the <code>$arg</code> parameter from the <code>method</code> this way. Because the <em>anonymous</em> function was declared outside of the <code>method</code>.</p>
  
  <p>But i really need a way to get the variables from the method where the anonymous function is run from. Is there a way to do that, when the anonymous function is declared somewhere else..?</p>