【问题标题】:Return via an abstract protected function?通过抽象保护函数返回?
【发布时间】:2010-04-19 17:08:30
【问题描述】:

我正在查看 Kohana 3 中的 auth 类以及登录脚本。当登录页面调用auth类的登录函数时,通过一个受保护的抽象函数_login返回。你为什么会出于好奇而这样做?我似乎无法理解真正有什么区别,因为无论哪种方式你都会返回相同的数据。在我脑海中浮现的一个选项是,通过受保护的摘要返回,您将确保数据从被放入 auth->login 函数和离开它的时间起没有被修改。我试图理解其中的一些细微差别。谢谢。

 public function login($username, $password, $remember = FALSE)
 {
  if (empty($password))
   return FALSE;

  if (is_string($password))
  {
   // Get the salt from the stored password
   $salt = $this->find_salt($this->password($username));

   // Create a hashed password using the salt from the stored password
   $password = $this->hash_password($password, $salt);
  }

  return $this->_login($username, $password, $remember);
 }

然后……

 abstract protected function _login($username, $password, $remember);

【问题讨论】:

    标签: php class function kohana


    【解决方案1】:

    这是模板方法设计模式的一个弱示例。每次有人想登录时,必须始终对变量进行多次修改和检查。密码不能为空。密码必须经过哈希处理。

    然后尝试登录用户。现在,这个函数之所以是抽象的,是因为登录例程可以以多种不同的方式实现;对于许多不同的数据库,有或没有会话等。

    这个函数被保护的原因是你不希望任何人直接调用 _login 的具体实现,而不首先运行之前的检查和修改(哈希密码等)。

    总而言之,它以这种方式编码,强制所有登录请求首先检查密码长度,然后对密码进行哈希处理,然后调用实际登录函数的具体实现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-17
      • 2010-11-08
      • 1970-01-01
      • 2013-11-13
      • 1970-01-01
      • 1970-01-01
      • 2015-08-02
      • 1970-01-01
      相关资源
      最近更新 更多