List all methods of a given class, excluding parent class's methods in PHP

前端 未结 4 1266
无人共我
无人共我 2021-02-14 03:51

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 g

4条回答
  •  离开以前
    2021-02-14 04:07

    $class_methods = get_class_methods('Bar');
    

    From the PHP Documenation

    This will not instantiate the class, and will allow you to get an array of all of the classes methods.

    I'm not completely certain that this won't return parent class methods, but get_class_methods will work for uninstantiated classes. If it does, you can use Alix's answer to remove the parent's method from the array. Or Lukman's to use the reverse engineering aspect of PHP internal code base to get the methods.


    BTW, if you type in new Bar(), it is going to create a new instance of Foo, as Bar extends Foo. The only way you can not instantiate Foo is by referring to it statically. Therefore, your request:

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

    Has no possible solution. If you give new Bar() as an argument, it will instantiate the class.

提交回复
热议问题