Skip to content
Advertisement

PHP – __call all the time

Say we have a class with several protected and/or public methods. I need to perform a check each time a method is called. I could do that check each time i call a method :

JavaScript

or

JavaScript

But i would like developers neither to have to think about it nor to write it. I’ve thought about using __call to do :

JavaScript

Unfortunatly, if i call the method from inside the class, __call will not invoked as it only works when a non-visible method is called.

Is there a clean way to hide this check for both internal and external calls ? Again the goal is to make sure a developper won’t forget to do it when calling a method.

Thanks in advance 🙂

JavaScript

I have another way of doing this :

JavaScript

But i won’t be able to use $this anymore, which means no protected methods, which i do need.

Advertisement

Answer

No, I dont think so. What you could do though is write a proxy:

JavaScript

This means you have to instantiate a proxy for every object you want to check:

JavaScript

Ofcourse you’d also want the proxy to behave exactly like the object itself. So you also have to define the other magic methods.

To make it a bit easier for the developers you could do something like this:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement