PHP从子类中获取重写的方法

Jos*_*tey 8 php reflection

鉴于以下情况:

<?php

class ParentClass {

    public $attrA;
    public $attrB;
    public $attrC;

    public function methodA() {}
    public function methodB() {}
    public function methodC() {}

}

class ChildClass extends ParentClass {

    public $attrB;

    public function methodA() {}
}
Run Code Online (Sandbox Code Playgroud)

如何获取在ChildClass中重写的方法列表(最好是类变量)?

谢谢,乔

编辑:修复坏延伸.任何方法,而不仅仅是公共方法.

Gor*_*don 14

反思是正确的,但你必须这样做:

$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
    }
}
Run Code Online (Sandbox Code Playgroud)

属性相同,只需使用即可getProperties().