我有一個方法,它通過使用將數組轉換為對象$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
- 1 回答
- 0 關注
- 115 瀏覽
添加回答
舉報
0/150
提交
取消