PHP get overridden methods from child class

后端 未结 2 760
不思量自难忘°
不思量自难忘° 2021-01-02 11:45

Given the following case:



        
2条回答
  •  天涯浪人
    2021-01-02 12:28

    Reflection is correct, but you would have to do it like this:

    $child  = new ReflectionClass('ChildClass');
    
    // find all public and protected methods in ParentClass
    $parentMethods = $child->getParentClass()->getMethods(
        ReflectionMethod::IS_PUBLIC ^ ReflectionMethod::IS_PROTECTED
    );
    
    // find all parent methods that were redeclared in ChildClass
    foreach($parentMethods as $parentMethod) {
        $declaringClass = $child->getMethod($parentMethod->getName())
                                ->getDeclaringClass()
                                ->getName();
    
        if($declaringClass === $child->getName()) {
            echo $parentMethod->getName(); // print the method name
        }
    }
    

    Same for Properties, just you would use getProperties() instead.

提交回复
热议问题