title: Passing variable to closure
body: <p>Lets say I have a closure:</p>
  
  <pre><code>$object-&gt;group(function() {
      $object-&gt;add('example');
      $object-&gt;add('example');
  });
  </code></pre>
  
  <p>It won't work because $object is not defined in the closure. </p>
  
  <p><strong>Notice: Undefined variable: manager</strong></p>
  
  <p>So I would have to <code>use ($object)</code>: </p>
  
  <pre><code>$object-&gt;group(function() use ($object) {
      $object-&gt;add('example');
      $object-&gt;add('example');
  });
  </code></pre>
  
  <p>Now I want to keep it as simple as the first one so somehow $object has to be injected in to the closure.</p>
  
  <p>The Laravel Framework does this with Routes for example:</p>
  
  <pre><code>Route::group(['middleware' =&gt; 'auth'], function () {
      Route::get('/', function ()    {
          // Uses Auth Middleware
      });
  
      Route::get('user/profile', function () {
          // Uses Auth Middleware
      });
  });
  </code></pre>
  
  <p>I believe Laravel does this with the <strong>Reflection</strong> class.</p>
  
  <p>How could I achieve this?</p>