PHP 8.5.8 Released!

ReflectionClass::getMethods

(PHP 5, PHP 7, PHP 8)

ReflectionClass::getMethods获取方法的数组

说明

public function ReflectionClass::getMethods(? int $filter = null ): array

获取类的方法的数组。

参数

filter

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

ReflectionMethod::IS_STATIC ReflectionMethod::IS_PUBLIC ReflectionMethod::IS_PROTECTED ReflectionMethod::IS_PRIVATE ReflectionMethod::IS_ABSTRACT ReflectionMethod::IS_FINAL 的按位或(OR),就会返回任意满足条件的属性。

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

返回值

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

更新日志

版本 说明
7.2.0 filter 现在允许为 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"
 }
}

参见

发现了问题?

了解如何改进此页面提交拉取请求报告一个错误
+添加备注

用户贡献的备注 3 notes

up
7
x_atrix at yahoo dot com
12 years ago
Note, for ReflectionClass::getMethods() not all methods in a final class are final, just the ones that have explicit modifier.
If you want to use an and operator for the filter, here is a simple implementation
<?php
final class Apple {
 public function publicMethod() { }
 public final function publicFinalMethod() { }
 protected final function protectedFinalMethod() { }
 private static function privateStaticMethod() { }
}
class MyReflection extends ReflectionClass {
 public function __construct($argument) {
 parent::__construct($argument);
 }
 
 /**
 * (non-PHPdoc)
 * @see ReflectionClass::getMethods()
 */
 public function getMethods($filter = null, $useAndOperator = true) {
 if ($useAndOperator !== true) {
 return parent::getMethods($filter);
 }
 
 $methods = parent::getMethods($filter);
 $results = array();
 
 foreach ($methods as $method) {
 if (($method->getModifiers() & $filter) === $filter) {
 $results[] = $method;
 }
 }
 
 return $results;
 }
}
$class = new MyReflection('Apple');
$methods = $class->getMethods(ReflectionMethod::IS_FINAL | ReflectionMethod::IS_PUBLIC);
var_dump($methods);
$methods = $class->getMethods(ReflectionMethod::IS_FINAL | ReflectionMethod::IS_PUBLIC, false);
var_dump($methods);
?>

Result:
array(1) {
 [0]=>
 object(ReflectionMethod)#4 (2) {
 ["name"]=>
 string(17) "publicFinalMethod"
 ["class"]=>
 string(5) "Apple"
 }
}
array(3) {
 [0]=>
 &object(ReflectionMethod)#5 (2) {
 ["name"]=>
 string(12) "publicMethod"
 ["class"]=>
 string(5) "Apple"
 }
 [1]=>
 &object(ReflectionMethod)#3 (2) {
 ["name"]=>
 string(17) "publicFinalMethod"
 ["class"]=>
 string(5) "Apple"
 }
 [2]=>
 &object(ReflectionMethod)#6 (2) {
 ["name"]=>
 string(20) "protectedFinalMethod"
 ["class"]=>
 string(5) "Apple"
 }
}
up
7
tom at r dot je
12 years ago
ReflectionClass::getMethods() sorts the methods by class (lowest in the inheritance tree first) then by the order they are defined in the class definition:
<?php
class A {
 public function method1() {
 
 }
 
 public function method2() {
 
 }
}
class B extends A {
 public function method3() {
 }
 public function method4() {
 }
}
$class = new ReflectionClass('B');
print_r($class->getMethods());
?>

This will output:
Array
(
 [0] => ReflectionMethod Object
 (
 [name] => method3
 [class] => B
 )
 [1] => ReflectionMethod Object
 (
 [name] => method4
 [class] => B
 )
 [2] => ReflectionMethod Object
 (
 [name] => method1
 [class] => A
 )
 [3] => ReflectionMethod Object
 (
 [name] => method2
 [class] => A
 )
)
up
1
deminy at deminy dot net
15 years ago
Method ReflectionClass::getMethods doesn't work constantly across different versions of PHP. For following code piece
<?php
class Dummy implements Iterator
{
 public function current () {}
 public function next () {}
 public function key () {}
 public function valid () {}
 public function rewind () {}
}
$reflection = new ReflectionClass('Dummy');
$aMethods = $reflection->getMethods();
echo '# of methods: ', count($aMethods), "\n";
?>

, it outputs "# of methods: 10" on PHP 5.2.14 and PHP 5.2.17, including all methods defined in the class itself and in the interface no matter if a method has been implemented or overridden; however, it returns "# of methods: 5" on PHP 5.3.5. Based on some other tests did by my colleagues, I assume it also returns "# of methods: 5" on PHP 5.2.10 and PHP 5.3.6.
+添加备注

AltStyle によって変換されたページ (->オリジナル) /