tags:

views:

49

answers:

3

Hi, I am trying to figure out, how to catch any method call on object in PHP. I know about magic function __call, but it is triggered only for method, that does not exist on called object.

For example i have something like this:

class Foo
{
  public function bar()
  {
    echo 'foobar';
  }

  public function override($method_name,$method_args)
  {
    echo 'Calling method ',$method_name,'<br />';
    $this->$method_name($method_args); //dirty, but working
  }
}

And when i do this:

$foo = new Foo();
$foo->bar();

I want this output:

Calling method bar
foobar

instead of this one:

foobar

Is there any way how to do this? Help please :)

+1  A: 

How about naming your real method _bar, catching the $foo->bar() call with __call, and then returning the result of $foo->_bar() ?

Adam
Yeah I had this idea before, but I need this for plugin system, and I don`t want to force users to write their plugins with _methods :)
Pirozek
+1  A: 

You can wrap an object around the object, intercepting any calls then forwarding them on the original object and returning the result.

Just store the object as a variable in your wrapper class and use overloading methods in your wrapper class to call/set/get/check on the object.

$object = new AnyObject;
$object = new Wrapper($object);

$object->anyMethod();
$object->anyVar = 'test';
echo $object->anyVar;
echo $object['array form'];

Looping the wrapper class in foreach is probably harder. Havent tried that.

OIS
Can you be more specific please? Just a small example :)
Pirozek
Thanks mate, this helped me.
Pirozek
+5  A: 

Taking your original Foo implementation you could wrap a decorator around it like this:

class Foo 
{
    public function bar() {
        echo 'foobar';
    }
}

class Decorator 
{
    protected $foo;

    function __construct(Foo $foo) {
       $this->foo = $foo;
    }

    function __call($method_name, $args) {
       echo 'Calling method ',$method_name,'<br />';
       return call_user_func_array(array($this->foo, $method_name), $args);
    }
}

$foo = new Decorator(new Foo());
$foo->bar();
ChristopheD
Thanks mate, thats exactly what i needed :)
Pirozek