我越來越未被捕獲的錯誤:調用未定義的方法stdClass的:: update()方法,即使該方法被明確限定
Uncaught Error: Call to undefined method stdClass::update()in /Applications/XAMPP/xamppfiles/htdocs/gallery/admin/includes/content.php:14 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/gallery/admin/index.php(11): require_once() #1 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/gallery/admin/includes/content.php on line 14.
然而,該方法update()
定義。下面是用戶等級:
class User {
public $username;
public $password;
public $first_name;
public $last_name;
public static function find_query($query) {
global $db;
$obj_arr = NULL;
if($db -> query($query) -> num_rows != 1) {
$users = $db -> query($query);
while($user = $users -> fetch_object()) {
$obj_arr[] = $user;
}
return $obj_arr;
} else {
return $db -> query($query) -> fetch_object();
}
}
public static function find_all() {
return self::find_query("SELECT * FROM users");
}
public static function get_user_byId($id) {
return self::find_query("SELECT * FROM users WHERE id=$id LIMIT 1");
}
public static function verify_user($username, $password) {
global $db;
$username = $db -> escape_string($username);
$password = $db -> escape_string($password);
$q = "SELECT * FROM users WHERE username='$username' AND password='$password' LIMIT 1";
return self::find_query($q);
}
public function create() {
global $db;
$username = $db -> escape_string($this -> username);
$password = $db -> escape_string($this -> password);
$first_name = $db -> escape_string($this -> first_name);
$last_name = $db -> escape_string($this -> last_name);
$q = "INSERT INTO users (username, password, first_name, last_name) ";
$q .= "VALUES ('$username', '$password', '$first_name', '$last_name')";
if($db -> query($q)) {
$this -> id = $db -> insert_id();
return true;
} else {
return false;
}
}
public function update() {
global $db;
$id = (int) $id;
$username = $db -> escape_string($this -> username);
$password = $db -> escape_string($this -> password);
$first_name = $db -> escape_string($this -> first_name);
$last_name = $db -> escape_string($this -> last_name);
$q = "UPDATE users SET username='$username', password='$password', first_name='$first_name', last_name='$last_name' ";
$q .= "WHERE id=$id";
//return (mysqli_affected_rows($db -> db) == 1) ? true:false;
return ($db -> db -> affected_rows == 1) ? true:false;
}
}
我呼籲更新,像這樣:
$user = User::get_user_byId(4);
$user -> password = 'newpass';
$user -> update();
其他方法做工精細,只是update()
。這可能是一個實例化問題嗎?如果這是一個愚蠢的問題,我很抱歉。我已經在這裏尋找答案,但找不到足以解決問題的任何事情。謝謝。
'返回$ DB的方法 - >查詢($查詢) - > fetch_object ();'返回一個'Standard Class'對象,而不是'User'對象 –
如果你想返回一個特定的對象類型,請查看[fetch_object()]的參數(http://www.php.net/手冊/ en/mysqli-result.fetch-object.php) –
啊...我如何讓它返回一個用戶類呢?我是OOP的新手。 –