列出给定类的所有方法,但不包括PHP中父类的方法 [英] List all methods of a given class, excluding parent class's methods in PHP

查看:273
本文介绍了列出给定类的所有方法,但不包括PHP中父类的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为PHP建立一个单元测试框架,我很好奇是否有一种方法可以获取排除父类方法的对象方法列表.因此,鉴于此:

I'm building a unit testing framework for PHP and I was curious if there is a way to get a list of an objects methods which excludes the parent class's methods. So given this:

class Foo
{

    public function doSomethingFooey()
    {
        echo 'HELLO THERE!';
    }
}

class Bar extends Foo
{
    public function goToTheBar()
    {
        // DRINK!
    }
}

我想要一个在给定参数new Bar()的情况下返回的函数:

I want a function which will, given the parameter new Bar() return:

array( 'goToTheBar' );

没有 需要实例化Foo的实例. (这意味着get_class_methods将不起作用).

推荐答案

使用 ReflectionClass ,例如:

$f = new ReflectionClass('Bar');
$methods = array();
foreach ($f->getMethods() as $m) {
    if ($m->class == 'Bar') {
        $methods[] = $m->name;
    }
}
print_r($methods);

这篇关于列出给定类的所有方法,但不包括PHP中父类的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆