PHP Conference Japan 2024

ReflectionClass::getMethods

(PHP 5, PHP 7, PHP 8)

ReflectionClass::getMethods取得方法的陣列

說明

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

取得類別的方法陣列。

參數

filter

過濾結果,僅包含具有特定屬性的方法。預設為不過濾。

任何ReflectionMethod::IS_STATICReflectionMethod::IS_PUBLICReflectionMethod::IS_PROTECTEDReflectionMethod::IS_PRIVATEReflectionMethod::IS_ABSTRACTReflectionMethod::IS_FINAL 的位元或運算組合,如此一來,所有具有任何給定屬性的方法都將被返回。

注意請注意,其他位元運算,例如 ~(反向)將無法如預期般運作。換句話說,例如,不可能擷取所有非靜態方法。

回傳值

一個包含 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 則註釋

x_atrix at yahoo dot com
11 年前
注意,對於 ReflectionClass::getMethods() 來說,並非 final 類別中的所有方法都是 final,只有明確標記為 final 的方法才是。
如果您想在篩選器中使用 and 運算子,這裡有一個簡單的實現
<?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);
}

/**
* (非 PHP 文件)
* @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);
?>

結果
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"
}
}
tom at r dot je
10 年前
ReflectionClass::getMethods() 會先依照類別排序方法(繼承樹中最底層的類別優先),然後再依照方法在類別定義中的順序排序。

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

這會輸出:

Array
(
[0] => ReflectionMethod Object
(
[name] => method3
[class] => B
)

[1] => ReflectionMethod Object
(
[name] => method4
[class] => B
)

[class] => B


(
[2] => ReflectionMethod Object
[name] => method1
)

[class] => A
(
[3] => ReflectionMethod Object
[name] => method1
)

)
deminy at deminy dot net
13 年前
ReflectionClass::getMethods 方法在不同 PHP 版本的行為並不一致。對於以下程式碼片段:

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

在 PHP 5.2.14 和 PHP 5.2.17 中,它會輸出「# of methods: 10」,包含類別本身和介面中定義的所有方法,無論方法是否已實作或覆寫;然而,在 PHP 5.3.5 中,它會返回「# of methods: 5」。根據我的同事進行的其他一些測試,我推測它在 PHP 5.2.10 和 PHP 5.3.6 中也會返回「# of methods: 5」。
To Top