10

我有一个由类定义的用于与我的网络应用程序交互的 API。每个可公开访问的方法都需要在运行前完成身份验证。我不想在每个方法中一遍又一遍地使用同一行,我想使用神奇的 __call 函数。但是,它只适用于私有或受保护的方法,而我的方法需要公开才能与 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.
  }
}

是否可以挂钩这些公共函数并可能在调用它们之前取消它们的执行?

4

2 回答 2

8

__call实际上适用于所有方法,包括公共。但是,如果公共方法已经存在,它将无法工作的原因是因为您的类之外的代码已经可以访问公共成员。__call仅对调用代码无法访问的成员调用。

据我所知,除了使用某种装饰器模式外,实际上没有其他选择可以满足您的需求:

class AuthDecorator {
    private $object;

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

    public function __call($method, $params) {
        //Put code for access checking here

        if($accessOk) {
            return call_user_func_array(array($this->object, $method), $params);
        }
    }
}

$api = new MY_Api();
$decoratedApi = new AuthDecorator($api);

//any calls to decoratedApi would get an auth check, and if ok, 
//go to the normal api class' function
于 2011-07-13T20:57:05.193 回答
3

由于您将我的评论作为可能的解决方案,因此我已将其格式化为后代的答案:

如果面向方面或装饰器的解决方案不适合您,您可以尝试更基于框架的解决方案,方法是将检查身份验证的代码放在公共方法的调用者中,甚至更高。

于 2011-07-13T22:18:34.160 回答