在 PHP 中,可以使用 get_class_methods()
函数来查看一个对象的方法。
示例代码:
class MyClass {
public function method1() {
// ...
}
private function method2() {
// ...
}
}
$obj = new MyClass();
$methods = get_class_methods($obj);
foreach ($methods as $method) {
echo $method . "\n";
}
运行以上代码,将会输出:
method1
注意,get_class_methods()
函数只能获取公共方法和受保护方法,不能获取私有方法。如果想要获取所有方法,包括私有方法,可以使用 ReflectionClass
类。
示例代码:
class MyClass {
public function method1() {
// ...
}
private function method2() {
// ...
}
}
$obj = new MyClass();
$reflection = new ReflectionClass($obj);
$methods = $reflection->getMethods();
foreach ($methods as $method) {
echo $method->getName() . "\n";
}
运行以上代码,将会输出:
method1
method2
上一篇:php bin2hex() 函数把 ASCII 字符的字符串转换为十六进制值。
下一篇:php如何访问父类方法
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站