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

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

    For anyone else wondering how to check if specific method belongs to specified class or its parent class, you can do this by getting its class name and then comparing it with actual class name like following.

    $reflection = new \ReflectionMethod($classInstance, 'method_name_here');
    if($reflection->class == MyClass::class)
    echo "Method belongs to MyClass";
    else
    echo "Method belongs to Parent classes of MyClass";
    

提交回复
热议问题