使用模型中的函数

时间:2012-11-01 03:33:45

标签: php yii

Users.php(型号):

...
public function hashPassword($password){
    $hashed = hash('sha256', $password . self::HASH_CODE);
    return $hashed;
}
...

UserIdentity.php

...
else if($user->password!==Users::hashPassword($this->password))
...

错误:

Non-static method Users::hashPassword() should not be called statically, assuming $this from incompatible context

3 个答案:

答案 0 :(得分:1)

您需要将hashPassword()定义为静态函数才能使用Users::hashPassword()调用它:

public static function hashPassword($password) {
    ...

否则,您可以创建Users类的实例并以非静态方式调用它:

$users = new Users();
$users->hashPassword($password);

严格来说 - yii意义上,您可以使用以下内容进行调用(具体取决于您的设置):

Yii::app()->Users->hashPassword($password);

答案 1 :(得分:1)

else if($user->password!==Users::model()->hashPassword($this->password))

这不是静态方法

答案 2 :(得分:1)

创建函数static

public static function hashPassword($password){
    $hashed = hash('sha256', $password . self::HASH_CODE);
    return $hashed; }