get_class_methods
(PHP 4, PHP 5, PHP 7, PHP 8)
get_class_methods — 返回由类的方法名组成的数组
参数
object_or_class
-
类名或者对象实例。
返回值
返回由 object_or_class
指定的类中定义的方法名所组成的数组。
更新日志
版本 | 说明 |
---|---|
8.0.0 |
object_or_class 参数现在只接受对象或者有效的类名。
|
示例
示例 #1 get_class_methods() 示例
<?php
class myclass {
// 构造方法
function __construct()
{
return(true);
}
// 方法 1
function myfunc1()
{
return(true);
}
// 方法 2
function myfunc2()
{
return(true);
}
}
$class_methods = get_class_methods('myclass');
// 或者
$class_methods = get_class_methods(new myclass());
foreach ($class_methods as $method_name) {
echo "$method_name\n";
}
?>
以上示例会输出:
__construct myfunc1 myfunc2
+添加备注
用户贡献的备注 4 notes
fschmengler at sgh-it dot eu ¶
15 years ago
It should be noted that the returned methods are dependant on the current scope. See this example:
<?php
class C
{
private function privateMethod()
{
}
public function publicMethod()
{
}
public function __construct()
{
echo '$this:';
var_dump(get_class_methods($this));
echo 'C (inside class):';
var_dump(get_class_methods('C'));
}
}
$c = new C;
echo '$c:';
var_dump(get_class_methods($c));
echo 'C (outside class):';
var_dump(get_class_methods('C'));
?>
Output:
$this:
array
0 => string 'privateMethod' (length=13)
1 => string 'publicMethod' (length=12)
2 => string '__construct' (length=11)
C (inside class):
array
0 => string 'privateMethod' (length=13)
1 => string 'publicMethod' (length=12)
2 => string '__construct' (length=11)
$c:
array
0 => string 'publicMethod' (length=12)
1 => string '__construct' (length=11)
C (outside class):
array
0 => string 'publicMethod' (length=12)
1 => string '__construct' (length=11)
polarglow06 at gmail dot com ¶
9 years ago
I have created a very simple test runner using this function
function get_bar($text) {
$bar = "";
for($i=1; $i<=strlen($text); $i++) {
$bar .= "=";
}
return $bar;
}
class Tester {
function __construct() {
$this->run_tests();
}
// run the tests
function run_tests() {
print("Tester by Minhajul Anwar \n");
$class = get_class($this);
$test_methods = preg_grep('/^test/', get_class_methods($this));
foreach($test_methods as $method) {
$start_rep = "test: $class::$method";
$bar = get_bar($start_rep);
print("\n$start_rep\n$bar\n");
$this->$method();
print("\n");
}
}
}
now you just need to write your test class with tegst methods prefixed by 'test', and then just instantiate object of that test class of your, all those tests methods will get run automatically
The drawback is: your test methods must not accept any arguments
an example:
require '../autoload.php';
register_autoload_paths(realpath('./'));
class Test_Test extends Tester {
function test_something() {
print("method got executed");
}
function testAnotherThing() {
print("another test method");
}
}
$Test = new Test_Test();
php at stock-consulting dot com ¶
18 years ago
Note that this function will answer both class AND instance methods ("class methods" are called "static" in PHP). Sort of a little "trap" for people who have in-depth experience with the OO terminology :-)
faizanakram99+php at gmail dot com ¶
2 months ago
It is worth noting that get_class_methods($closure) doesn't return __invoke method even though it exists and is documented too.
See https://3v4l.org/VKjAF
备份地址:http://www.lvesu.com/blog/php/function.get-class-methods.php