如何在PHP中捕获对象的任何方法调用?

时间:2022-10-13 11:58:02

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

我试图弄清楚如何捕获在PHP中调用对象的任何方法。我知道魔术函数__call,但它仅针对被调用对象上不存在的方法触发。

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 :)

有什么方法可以做到这一点吗?请帮助 :)

3 个解决方案

#1


41  

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

采用原始的Foo实现,你可以将装饰器包裹起来,如下所示:

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

class Decorator 
{
    protected $foo;

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

    public 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();

#2


2  

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.

在foreach中循环包装类可能更难。没试过。

#3


0  

If you set the function to private , call will trap any call to it from the outside will be trapped in __call, but you can call it from the inside

如果你把这个功能设置为私有,那么调用会从外面捕获任何对它的调用都会被困在__call中,但你可以从里面调用它

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

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

#1


41  

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

采用原始的Foo实现,你可以将装饰器包裹起来,如下所示:

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

class Decorator 
{
    protected $foo;

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

    public 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();

#2


2  

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.

在foreach中循环包装类可能更难。没试过。

#3


0  

If you set the function to private , call will trap any call to it from the outside will be trapped in __call, but you can call it from the inside

如果你把这个功能设置为私有,那么调用会从外面捕获任何对它的调用都会被困在__call中,但你可以从里面调用它

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

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