如何获取反射class方法中的参数类型(PHP 5.x)?
How to get parameter type in reflected class method (PHP 5.x)?
我正在尝试获取类型 $bar
变量。
<?php
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 Reflection - Get Method Parameter Type As String
我写了适用于所有情况的解决方案:
/**
* @param ReflectionParameter $parameter
* @return string|null
*/
function getParameterType(ReflectionParameter $parameter)
{
$export = ReflectionParameter::export(
array(
$parameter->getDeclaringClass()->name,
$parameter->getDeclaringFunction()->name
),
$parameter->name,
true
);
return preg_match('/[>] ([A-z]+) /', $export, $matches)
? $matches[1] : null;
}
如果您只是类型提示 类,您可以使用 ->getClass()
,它在 PHP 5 和 7 中受支持。
<?php
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());
}
}
我说 类 的原因是因为在数组上,它将 return NULL。
object(ReflectionClass)#6 (1) {
["name"]=>
string(8) "stdClass"
}
NULL
object(ReflectionClass)#6 (1) {
["name"]=>
string(7) "MyClass"
}
我正在尝试获取类型 $bar
变量。
<?php
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 Reflection - Get Method Parameter Type As String
我写了适用于所有情况的解决方案:
/**
* @param ReflectionParameter $parameter
* @return string|null
*/
function getParameterType(ReflectionParameter $parameter)
{
$export = ReflectionParameter::export(
array(
$parameter->getDeclaringClass()->name,
$parameter->getDeclaringFunction()->name
),
$parameter->name,
true
);
return preg_match('/[>] ([A-z]+) /', $export, $matches)
? $matches[1] : null;
}
如果您只是类型提示 类,您可以使用 ->getClass()
,它在 PHP 5 和 7 中受支持。
<?php
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());
}
}
我说 类 的原因是因为在数组上,它将 return NULL。
object(ReflectionClass)#6 (1) {
["name"]=>
string(8) "stdClass"
}
NULL
object(ReflectionClass)#6 (1) {
["name"]=>
string(7) "MyClass"
}