请注意,对于 ReflectionClass::getMethods(),最终类中的并非所有方法都是最终方法,只有那些具有显式修饰符的方法才是最终方法。
如果您想对过滤器使用与运算符,这里有一个简单的实现
<?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);
}
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"
}
}