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

前端 未结 4 1265
无人共我
无人共我 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:06

    You can use get_class_methods() without instantiating the class:

    $class_name - The class name or an object instance.

    So the following would work:

    $bar_methods = array_diff(get_class_methods('Bar'), get_class_methods('Foo'));
    

    Assuming there aren't repeated methods in the parent class. Still, Lukman's answer does a better job. =)

提交回复
热议问题