views:

58

answers:

2

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().

A: 

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.

Amber
Can it be done using Reflection?
Alix Axel
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: http://www.php.net/manual/en/reflectionmethod.getdeclaringclass.php
Amber
+1  A: 

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)))
Ewan Todd
That might not return the right results if the class doing the extending overrode a method in the extended class, though.
Amber
Thanks Ewan, the solution you provided is enough for me.
Alix Axel