测试类/方法是否具有带反射类的类型提示参数的正确方法是什么。
我如何测试它是否存在,是否有类型暗示而没有抛出错误。
如果没有打字,我的sofar会返回通知。
Notice: Trying to get property of non-object in
(如果我使用)getClass()->getName();
$p = new ReflectionParameter(array(get_called_class(), $method), 0);
if ($p) { // not working
echo "param exists";
答案 0 :(得分:0)
这就是你在寻找什么?
interface CommandInterface
{
}
class Dummy
{
public function execute(
CommandInterface $command,
$when = 'now',
$delayed = true,
$append = null
) {
//
}
}
$reflector = new \ReflectionClass('Dummy');
foreach($reflector->getMethod('execute')->getParameters() as $param)
{
$type = ($param->getClass()) ?
$param->getClass()->name :
gettype($param->getDefaultValue());
echo sprintf('Parameter "%s" of type "%s"'.PHP_EOL, $param->getName(), $type);
}
输出:
Parameter "command" of type "CommandInterface"
Parameter "when" of type "string"
Parameter "delayed" of type "boolean"
Parameter "append" of type "NULL"