(PHP 5, PHP 7, PHP 8)
ReflectionClass::getMethods — 獲取方法的數(shù)組
$filter = ?): array獲取類(lèi)的方法的一個(gè)數(shù)組。
filter過(guò)濾結(jié)果為僅包含某些屬性的方法。默認(rèn)不過(guò)濾。
ReflectionMethod::IS_STATIC、
ReflectionMethod::IS_PUBLIC、
ReflectionMethod::IS_PROTECTED、
ReflectionMethod::IS_PRIVATE、
ReflectionMethod::IS_ABSTRACT、
ReflectionMethod::IS_FINAL 的按位或(OR),就會(huì)返回任意滿(mǎn)足條件的屬性。
注意: 請(qǐng)注意:其他位操作,例如
~無(wú)法按預(yù)期運(yùn)行。這個(gè)例子也就是說(shuō),無(wú)法獲取所有的非靜態(tài)方法。
包含每個(gè)方法 ReflectionMethod 對(duì)象的數(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() 中過(guò)濾結(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"
}
}