title: PHP - Pass a function into another function and call it
body: <p>Is it possible to pass a function to another function and then call it?
  Example:</p>
  
  <pre><code>class TestController extends Controller
  {       
      public function sayHello()
      {   
          echo "Hello";
      }
  
      public function sayBye()
      {   
          echo "Bye";
      }
  
      public function say($text)
      {
           if ($text === "Hello") {
                   $this-&gt;doChecks($text, $this-&gt;sayHello);
           }
           else {
                   $this-&gt;doChecks($text, $this-&gt;sayBye);
           }
      }
  
      public function doChecks($text, $callback)
      {
           if (is_string($text)) {
                   $callback();
           }
           else {
               echo "ERROR";
           }
      }
  }
  </code></pre>
  
  <p>If you call <code>say()</code> like this <code>say('Hello')</code> then <code>sayHello()</code> should be executed, after the check in <code>doChecks()</code> were passed, otherwise (if $text is not a string) "<strong>ERROR</strong>" should be printed.</p>
  
  <p><strong>What I get</strong>: <code>Undefined property: App\Http\Controllers\TestController::$sayHello</code></p>
  
  <p>If you call <code>say()</code> like this <code>say('somerandomstuff')</code> then <code>sayBye()</code> should be executed, after the check in <code>doChecks()</code> were passed, otherwise (if $text is not a string) "<strong>ERROR</strong>" should be printed.</p>
  
  <p><strong>What I get</strong>: <code>Undefined property: App\Http\Controllers\TestController::$sayBye</code></p>
  
  <hr>
  
  <p>If I try to pass the functions address like this:</p>
  
  <pre><code> public function doChecks($text, &amp;$callback)
  </code></pre>
  
  <p>then I get <code>Function name must be a string</code>, if I output $callback with echo inside <code>doChecks</code> like this: <code>echo "'$callback'";</code> then I get ''</p>
  
  <p>How does it work?</p>