1

I've this sample code:

class A
{
 public function A_A() { /* ... */ }

 public function A_B() { /* ... */ }
}

class B extends A
{
 public function B_A() { /* ... */ }

 public function B_B() { /* ... */ }

 public function B_C()
 {
  return get_class_methods($this);
 }
}

$a = new A();
$b = new B();

Doing this:

echo '<pre>';
print_r($b->B_C());
echo '</pre>';

Yields the following output:

Array
(
    [0] => B_A
    [1] => B_B
    [2] => B_C
    [3] => A_A
    [4] => A_B
)

How can I make it return only the following methods?

Array
(
    [0] => B_A
    [1] => B_B
    [2] => B_C
)

I've a method in class A that should call all the methods in class B, the problem is of course that it leads to a infinite loop due to the values returned by get_class_methods().

2 Answers 2

3

You might need full strength Reflection. However, before you go there, it might be worth trying something like this.

array_diff(get_class_methods($this), get_class_methods(get_parent_class($this)))
Sign up to request clarification or add additional context in comments.

2 Comments

That might not return the right results if the class doing the extending overrode a method in the extended class, though.
Thanks Ewan, the solution you provided is enough for me.
0

You can't. Part of the functionality of extending a class is that you get all of the methods of the class you extended, in the new class, identical to as if you defined them in the new class itself.

1 Comment

You might be able to check ReflectionMethod's getDeclaringClass() result to check and see if the current class declared it, but I'm not sure whether an extended class returns itself or what it extended from for inherited methods: php.net/manual/en/reflectionmethod.getdeclaringclass.php

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.