8

Given the following case:

<?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() {}
}

How can I get a list of methods (and preferably class vars) that are overridden in ChildClass?

Thanks, Joe

EDIT: Fixed bad extends. Any methods, not just public ones.

1
  • 2
    I think you mean class ChildClass extends ParentClass Commented Apr 19, 2010 at 16:39

2 Answers 2

15

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This is the way. Just use getMethods() then foreach through them calling getDeclaringClass()->getName(). If name != class name, then it's been overriden. The same can be done with getProperties()
lol. I didn't realize it was a work in progress or I wouldn't have added my comment.
@webbiedave I felt it was still too abstract to grasp. It's quite easy when you know how to do it but for someone new to the API it might be difficult. Plus, it was a nice exercise :)
4

You can use ReflectionClass to achieve this:

$ref = new ReflectionClass('ChildClass');

print_r($ref->getMethods());
print_r($ref->getProperties());

This will output:

Array
(
    [0] => ReflectionMethod Object
        (
            [name] => methodA
            [class] => ChildClass
        )

)

Array
(
    [0] => ReflectionProperty Object
        (
            [name] => attrB
            [class] => ChildClass
        )

)

See the manual for more useful information on reflection: https://www.php.net/manual/en/class.reflectionclass.php

4 Comments

@Janci: +1 for reflection class and yes it is very useful :)
I don't think this would work if ChildClass was actually declared as extending ParentClass
This would also return any regular added classes and properties that do not overwrite a parent method or property
@Tom: You are right, I overlooked that in the initial example, the ChildClass did not extend ParentClass, and just copied it as it was. Shall the ChildClass actually extend ParentClass, my solution will return all methods including those in parent class. Sorry about that. The correct solution involves iterating through returned methods and comparing $method->getDeclaringClass()->getName() with the class name you are interested in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.