PHPTS:一键免费搭建 Nginx + PHP + MySQL 运行环境

ReflectionClass::getMethods

(PHP 5, PHP 7)

ReflectionClass::getMethods获取方法的数组

说明

public ReflectionClass::getMethods ([ int $filter ] ) : array

获取类的方法的一个数组。

参数

filter

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

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

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

返回值

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

范例

Example #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"
  }
}

Example #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"
  }
}

参见