可能重複時:
PHP Reflection - Get Method Parameter Type As String類型提示在PHP調用任意函數(封閉件)
捷希凱PHP microframework確實基於自動類型提示回調注射。例如,在Silex中,可以使用任意參數提供Closure參數,如下所示:
$app->get('/blog/show/{postId}/{commentId}', function ($commentId, $postId) {
//...
});
$app->get('/blog/show/{id}', function (Application $app, Request $request, $id) {
//...
});
// following works just as well - order of arguments is not important
$app->get('/blog/show/{id}', function (Request $request, Application $app, $id) {
//...
});
我該如何做?我不想將參數類型作爲字符串獲取。我正在尋找一種「無弦」全自動解決方案。換句話說,
對於許多的可能的參數:
$possible_arguments = [ new Class_A(), new Class_B(), new Class_C(), new Another_Class, $some_class ];
對於具有任意數量的任意參數的閉合,即可以只包括上面只有一次定義的那些:
$closure = function (Class_B $b, Another_Class, $a) { // Do something with $a and $b };
我只需要獲取匹配的參數以便調用與他們關閉:
// $arguments is now [$possible_arguments[1], $possible_arguments[3]] call_user_func_array($closure, $arguments);
他們將使用[思考](http://php.net/manual/en/ book.reflection.php)。閉包在內部轉換爲[Closure](http://php.net/manual/en/class.closure.php)類的實例,反射對其就像任何其他對象一樣工作。你應該能夠通過檢查對象的__invoke()方法來獲得閉包的參數列表。 – DaveRandom