小智 28
是的,你可以看看反射类/方法.
http://php.net/manual/en/book.reflection.php和 http://www.php.net/manual/en/reflectionclass.getmethods.php
$class = new ReflectionClass('Apple');
$methods = $class->getMethods(ReflectionMethod::IS_PUBLIC);
var_dump($methods);
Run Code Online (Sandbox Code Playgroud)
Die*_*lló 14
由于get_class_methods()
是范围敏感,你可以通过调用类范围外的函数得到一个类的所有公共方法:
所以,拿这堂课:
class Foo {
private function bar() {
var_dump(get_class_methods($this));
}
public function baz() {}
public function __construct() {
$this->bar();
}
}
Run Code Online (Sandbox Code Playgroud)
var_dump(get_class_methods('Foo'));
将输出以下内容:
array
0 => string 'baz' (length=3)
1 => string '__construct' (length=11)
Run Code Online (Sandbox Code Playgroud)
来自class(new Foo;
)范围内的调用将返回:
array
0 => string 'bar' (length=3)
1 => string 'baz' (length=3)
2 => string '__construct' (length=11)
Run Code Online (Sandbox Code Playgroud)
在获得所有方法之后,get_class_methods($theClass)
可以使用以下内容循环遍历它们:
foreach ($methods as $method) {
$reflect = new ReflectionMethod($theClass, $method);
if ($reflect->isPublic()) {
}
}
Run Code Online (Sandbox Code Playgroud)