How to catch any method call on object in PHP?

前端 未结 3 589
予麋鹿
予麋鹿 2020-12-03 02:38

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

3条回答
  •  爱一瞬间的悲伤
    2020-12-03 03:22

    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.

提交回复
热议问题