title: PHP: Pass anonymous function as argument
body: <p>Is it possible to pass an anonymous function as an argument, and have it execute immediately, thus passing the function's <code>return</code> value?</p>
  
  <pre><code>function myFunction(Array $data){
      print_r($data);
  }
  
  myFunction(function(){
      $data = array(
          'fruit'     =&gt; 'apple',
          'vegetable' =&gt; 'broccoli',
          'other'     =&gt; 'canned soup');
      return $data;
  });
  </code></pre>
  
  <p>This throws an error due to the <code>Array</code> type-hint, complaining of an object being passed. Alright, if I remove the type-hint, it of course spits out <code>Closure Object</code>, rather than the results I want. I understand that I am technically passing an object instance of <code>Closure</code> to <code>myFunction</code>, however, I'm near certain that I've seen this accomplished elsewhere. Is this possible? If so, what am I doing wrong?</p>
  
  <p>For the sake of this discussion, I cannot modify the function to which I'm passing the closure.</p>
  
  <p><strong>tl;dr: How can I pass an anonymous function declaration as an argument, resulting in the return value being passed as the argument.</strong></p>
  
  <p>PS: If not clear, the desired output is:</p>
  
  <pre><code>Array
  (
      [fruit] =&gt; apple
      [vegetable] =&gt; broccoli
      [other] =&gt; canned soup
  )
  </code></pre>