I'm trying to understand the popular use of PHP closure/ callback of functions in routing requests within modern frameworks. Slim for example, allows you to do the following:
$app->get('/hello/:name', function ($name) {
echo "Hello, $name";
});
$app->run();
My experiment:
class Foo
{
private $string;
private $callback;
public function get($string, $callback)
{
$this->string = $string;
$this->callback = $callback;
}
public function run()
{
return $this->get($this->string, $this->callback);
}
}
$Foo = new Foo;
$Foo->get('/world', function($name) {
return "Hello " . $name;
});
$Foo->run();
How can execute the callback in the class so it return Hello World?