Php 在Silex中将类方法作为回调传递
是否可以在Silex中将类/对象方法作为回调传递?例如,我有以下代码:Php 在Silex中将类方法作为回调传递,php,callback,silex,Php,Callback,Silex,是否可以在Silex中将类/对象方法作为回调传递?例如,我有以下代码: class Implementation { public function test() { return "Hello World!"; } } $code = new Implementation(); $app->get('/', function() use ($code) { return $code->test(); }); 因为我不希望匿名回调中包含代码,所以我将其移动到了一个类中
class Implementation {
public function test() {
return "Hello World!";
}
}
$code = new Implementation();
$app->get('/', function() use ($code) {
return $code->test();
});
因为我不希望匿名回调中包含代码,所以我将其移动到了一个类中。这是可行的,但是我想知道是否有更简单的方法来写这个,比如:
$app->get('/', $code->test);
到目前为止,我还没有发现任何东西,所以如果你们中有人有想法,我非常感激:)您正在寻找:
Vielen Dank!:)我不知道我怎么会错过这个。。。可能是因为我在PHP中寻找它,而不是Silex。。。
$app->get('/', 'Acme\\Foo::bar');
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
namespace Acme
{
class Foo
{
public function bar(Request $request, Application $app)
{
// ...
}
}
}