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