php 参数类型获取,如何在反射类方法(PHP 5.x)中获取参数类型?

我正在尝试输入$bar变量.

class Foo

{

public function test(stdClass $bar, array $foo)

{

}

}

$reflect = new ReflectionClass('Foo');

foreach ($reflect->getMethods() as $method) {

foreach ($method->getParameters() as $num => $parameter) {

var_dump($parameter->getType());

}

}

我期待stdClass,但我得到了

Call to undefined method ReflectionParameter::getType()

有什么不对?还是有另一种方式?…

$php -v

PHP 5.4.41 (cli) (built: May 14 2015 02:34:29)

Copyright (c) 1997-2014 The PHP Group

Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies

UPD1

它也适用于数组类型.

解决方法:

如果您只是键入提示类,则可以使用PHP 5和7中支持的->getClass().

class MyClass {

}

class Foo

{

public function test(stdClass $bar)

{

}

public function another_test(array $arr) {

}

public function final_test(MyClass $var) {

}

}

$reflect = new ReflectionClass('Foo');

foreach ($reflect->getMethods() as $method) {

foreach ($method->getParameters() as $num => $parameter) {

var_dump($parameter->getClass());

}

}

我说类的原因是因为在数组上,它将返回NULL.

object(ReflectionClass)#6 (1) {

["name"]=>

string(8) "stdClass"

}

NULL

object(ReflectionClass)#6 (1) {

["name"]=>

string(7) "MyClass"

}

标签:php-5-4,php,reflection,parameters

来源: https://codeday.me/bug/20190829/1759272.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值