0
votes

Given class A that extends class B, how can I have calls to class A's __call function override the matching function inherited from the parent?

Consider this simplified example:

class A
{
    public function method_one()
    {
        echo "Method one!\n";
    }
}
class B extends A
{
    public function __call($name, $args)
    {
        echo "You called $name!\n";
    }
}
$b = new B();
$b->method_one();

When I run it, I get the output Method one!. I WANT to get the output You called method_one!.

So, how do I have the subclass's magic method override the parent classes defined method?

I need to extend the object, because I need access to a protected method in A, but I want to channel all public methods into my own __call handler. Is there any way to do this?

3
__call only overrides methods that aren't otherwise accessible; your method_one() is public, so it's accessible. Try making it protectedMark Baker
Why don't you just use a normal override ?Shankar Narayana Damodaran
@ShankarDamodaran because I have a large number of functions that I want to override. The only reason I'm extending it in the first place, is to get access to the protected method I needBenubird
You could probably use php.net/manual/en/reflectionmethod.setaccessible.php to change the visibility of your methods so that they were no longer publicly accessible, so that they'd be called through the __call handlerMark Baker
@MarkBaker Yes! That is the perfect answer. Thank you!Benubird

3 Answers

0
votes

So, I found a way to do it, which involves making an intermediate class to expose the protected method I need, while still using __call for the public ones. This works, but I really don't like the idea of extending a class just to expose a protected method... Still, someone might find it useful, so thought I'd share:

class A
{
    public function method_one()
    {
        echo "Method one!\n";
    }
    protected function protected_method()
    {
        echo "Accessible!\n";
    }
}
class A_accessor extends A
{
    public function publicise()
    {
        $args = func_get_args();
        return call_user_func_array(array($this, array_shift($args)), $args);
    }
}
class B
{
    private $A;

    public function __construct()
    {
        $this->A = new A_accessor();
    }

    public function __call($name, $args)
    {
        echo "You called $name!\n";
    }

    public function protected_method()
    {
        return $this->A->publicise('protected_method');
    }
}
$b = new B();
$b->method_one();
$b->protected_method();
0
votes

This is the answer I actually used, based on Mark Baker's comment.

By having an object of the class whose methods I want access to as a variable, I can use ReflectionMethod to access any of its methods just as if I was extending it, but with __call still catching everything else. So any methods I want to pass through, I can pass through with something like this:

public function __call($name, $args)
{
    $method = new ReflectionMethod($this->A, $name);
    $method->setAccessible(true);
    return $method->invokeArgs($this->A, $args);
}

Or in my case, with the full class like this:

class B
{
    private $A;

    public function __construct()
    {
        $this->A = new A();
    }

    public function __call($name, $args)
    {
        echo "You called $name!\n";
    }

    public function protected_method()
    {
        $method = new ReflectionMethod($this->A, 'protected_method');
        $method->setAccessible(true);
        return $method->invoke($this->A, $args);
    }
}
0
votes

try this

 class A1
{
    protected function method_one()
    {
       echo "Method one!\n";
    }
}
class B1
{
    private $A;
    public function __construct()
    {
       $this->A = new A1;
    }
    public function __call($name, $args)
   {
      $class = new ReflectionClass($this->A);
      $method = $class->getMethod($name);
      $method->setAccessible(true);
      //return $method;
      echo "You called $name!\n";
      $Output=$method->invokeArgs($this->A, $args);
      $method->setAccessible(false);
      return $Output;
  }
 }

$a = new B1;
$a->method_one("");