title: How do you use an anonymous function inside a class in PHP?
body: <p>How do I call <code>$greet</code> inside this class?  I am using PHP 5.5.4.</p>
  
  <pre><code>&lt;?PHP   
  class Model
  {
      public $greet = function($name)
      {
          printf("Hello %s\r\n", $name);
      };
  }
  
  $test = new Model();
  $test-&gt;greet('World');
  $test-&gt;greet('PHP');
  ?&gt;
  
  Parse error: syntax error, unexpected '$greet' (T_VARIABLE), expecting function (T_FUNCTION)
  </code></pre>
  
  <p>Also tried this,</p>
  
  <pre><code>$test = new Model();
  call_user_func($test-&gt;greet('World'));
  call_user_func($test-&gt;greet('PHP')) 
  </code></pre>
  
  <p>The anonymous function works fine outside the class (straight from the <a href="http://php.net/manual/en/functions.anonymous.php" rel="noreferrer">manual</a>).</p>
  
  <pre><code>&lt;?php
  $greet = function($name)
  {
      printf("Hello %s\r\n", $name);
  };
  
  $greet('World');
  $greet('PHP');
  ?&gt;
  </code></pre>
  
  <p>EDIT:  I took out the dollar signs, in my call (I caught it just as an answer started to roll in.  It did not help,</p>
  
  <pre><code>call_user_func($test-&gt;greet('World'));
  call_user_func($test-&gt;greet('PHP'));
  </code></pre>
  
  <p>EDIT:</p>
  
  <pre><code>class Model
  {
      public $greet;
      function __construct()
      {
          $this-&gt;greet = function($name)
          {
              printf("Hello %s\r\n", $name);
          };
      }
  }
  
  $test = new Model();
  $test-&gt;greet('johnny');
  </code></pre>
  
  <p>Now I get,</p>
  
  <pre><code>Fatal error: Call to undefined method Model::greet() 
  </code></pre>