亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有選項可以在 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 關注
  • 115 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號