2012-11-01 34 views
0

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 

回答

1

你需要定義hashPassword()爲靜態功能以便用Users::hashPassword()調用它:

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

否則,您可以創建Users類的實例,並調用它在一個非靜態的方式:

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

在strictly- yii感,你可以用下面的調用它(根據您的設置):

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

讓它變成靜態是最好的方法嗎? –

+0

@LucianoNascimento由於該方法的目的,是的 - 它看起來像是一個很好的靜態候選人。 – newfurniturey

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

這不是一個靜態方法

1

製作功能static

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