ReflectionClass::getMethods

(PHP 5, PHP 7, PHP 8)

ReflectionClass::getMethods获取方法的数组

说明

publicReflectionClass::getMethods(?int$filter = null): array

获取类的方法的数组。

参数

filter

过滤结果为仅包含某些属性的方法。默认不过滤。

ReflectionMethod::IS_STATICReflectionMethod::IS_PUBLICReflectionMethod::IS_PROTECTEDReflectionMethod::IS_PRIVATEReflectionMethod::IS_ABSTRACTReflectionMethod::IS_FINAL 的按位或(OR),就会返回任意满足条件的属性。

注意: 请注意:其他位操作,例如 ~ 无法按预期运行。这个示例也就是说,无法获取所有的非静态方法。

返回值

包含每个方法 ReflectionMethod 对象的数组

更新日志

版本说明
7.2.0filter 现在允许为 null。

示例

示例 #1 ReflectionClass::getMethods() 的基本用法

<?php
class Apple {
public function
firstMethod() { }
final protected function
secondMethod() { }
private static function
thirdMethod() { }
}

$class = new ReflectionClass('Apple');
$methods = $class->getMethods();
var_dump($methods);
?>

以上示例会输出:

array(3) { [0]=> object(ReflectionMethod)#2 (2) { ["name"]=> string(11) "firstMethod" ["class"]=> string(5) "Apple" } [1]=> object(ReflectionMethod)#3 (2) { ["name"]=> string(12) "secondMethod" ["class"]=> string(5) "Apple" } [2]=> object(ReflectionMethod)#4 (2) { ["name"]=> string(11) "thirdMethod" ["class"]=> string(5) "Apple" } }

示例 #2 从 ReflectionClass::getMethods() 中过滤结果

<?php
class Apple {
public function
firstMethod() { }
final protected function
secondMethod() { }
private static function
thirdMethod() { }
}

$class = new ReflectionClass('Apple');
$methods = $class->getMethods(ReflectionMethod::IS_STATIC | ReflectionMethod::IS_FINAL);
var_dump($methods);
?>

以上示例会输出:

array(2) { [0]=> object(ReflectionMethod)#2 (2) { ["name"]=> string(12) "secondMethod" ["class"]=> string(5) "Apple" } [1]=> object(ReflectionMethod)#3 (2) { ["name"]=> string(11) "thirdMethod" ["class"]=> string(5) "Apple" } }

参见

To Top