为了账号安全,请及时绑定邮箱和手机立即绑定

有选项可以在 PHP 中获取预期的变量类型吗?

有选项可以在 PHP 中获取预期的变量类型吗?

PHP
蝴蝶刀刀 2023-04-15 14:25:37
我有一个方法,它通过使用将数组转换为对象$class = get_class($object); $methodList = get_class_methods($class);但现在我也需要有关预期变量类型的信息。例如从这个方法:public function setFoo(int $foo){ }我也需要得到int。有没有办法得到它?
查看完整描述

1 回答

?
GCT1015

TA贡献1827条经验 获得超4个赞

您可以使用反射。具体来说ReflectionParameter::getType()

function someFunction(int $param, $param2) {}


$reflectionFunc = new ReflectionFunction('someFunction');

$reflectionParams = $reflectionFunc->getParameters();

$reflectionType1 = $reflectionParams[0]->getType();

$reflectionType2 = $reflectionParams[1]->getType();


assert($reflectionType1 instanceof ReflectionNamedType);

echo $reflectionType1->getName(), PHP_EOL;

var_dump($reflectionType2);

上面的例子会输出:


int

NULL


查看完整回答
反对 回复 2023-04-15
  • 1 回答
  • 0 关注
  • 73 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信