I have an API for interacting with my web app, defined by a class. Each publicly accessible method needs to have authentication done before running. Rather than putting the same line over and over in each method, I’d like to use the magic __call function. However, it will only work on private or protected methods, and mine need to be public in order to work with Zend_Json_Server.
class MY_Api
{
public function __call($name, $arguments)
{
//code here that checks arguments for valid auth token and returns an error if false
}
public function myFunction($param1, $param2, $param3)
{
//do stuff when the user calls the myFunction and passes the parameters
//this function must remain public so that Zend_Json_Server can parse it
//but I want it intercepted by a magic method so that the authentication
//can be checked and the system bails before it even gets to this function.
}
}
Is it possible to hook into these public functions and possibly cancel their execution before they are called?
__callactually works for all methods, including public. However, the reason it won’t work if the public method already exists, is because code outside your class can already access the public members.__callis only invoked for members which aren’t accessible by the calling code.As far as I know there are really no options to do what you’re looking for, except by using some kind of a decorator pattern: