ReflectionMethod::getParameters
返回一个对象数组ReflectionParameter
。ReflectionParameters 有一个调用方法,该方法getClass
将返回有关参数类型提示的信息。
例子:
<?php
interface Y { }
class X
{
public function __construct(Y $x, $y=null)
{
}
}
$ref = new \ReflectionClass('X');
$c = $ref->getConstructor();
foreach ($c->getParameters() as $p) {
var_dump($p->getClass());
}
输出:
class ReflectionClass#5 (1) {
public $name =>
string(1) "Y"
}
NULL
SilexControllerResolver
有一个很好的例子来说明你如何使用它:
<?php
// $params is an array of ReflectionParameter instances
protected function doGetArguments(Request $request, $controller, array $parameters)
{
foreach ($parameters as $param) {
// check to see if there's a class and if there is, see if the app property
// is the same type. If so, set the attribute on the request
if ($param->getClass() && $param->getClass()->isInstance($this->app)) {
$request->attributes->set($param->getName(), $this->app);
break;
}
}
return parent::doGetArguments($request, $controller, $parameters);
}