ReflectionClass::getMethods

(PHP 5, PHP 7, PHP 8)

ReflectionClass::getMethods獲取方法的數(shù)組

說明

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

獲取類的方法的一個(gè)數(shù)組。

參數(shù)

filter

過濾結(jié)果為僅包含某些屬性的方法。默認(rèn)不過濾。

ReflectionMethod::IS_STATICReflectionMethod::IS_PUBLIC、 ReflectionMethod::IS_PROTECTED、 ReflectionMethod::IS_PRIVATEReflectionMethod::IS_ABSTRACT、 ReflectionMethod::IS_FINAL 的按位或(OR),就會(huì)返回任意滿足條件的屬性。

注意: 請注意:其他位操作,例如 ~ 無法按預(yù)期運(yùn)行。這個(gè)例子也就是說,無法獲取所有的非靜態(tài)方法。

返回值

包含每個(gè)方法 ReflectionMethod 對象的數(shù)組

范例

示例 #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);
?>

以上例程會(huì)輸出:

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() 中過濾結(jié)果

<?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);
?>

以上例程會(huì)輸出:

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

參見