我有中創建用戶類的對象,但如果用戶沒有找到我需要返回的東西的方法,但我不知道是什麼:我應該返回false,異常等
function getUserById($id)
{
$sth = $this->db->prepare("SELECT id, username, password, salt, email, created, last_active FROM users WHERE id = ?");
$sth->execute(array($id));
$sth->setFetchMode(PDO::FETCH_OBJ);
if ($sth->rowCount() == 0) {
//return what?
}
$row = $sth->fetch();
$user = new User($row);
return $user;
}
而且方法應該是使用這樣的事:
$user = $user_mapper->getUserById($id);
的問題是我應該如何處理如果沒有找到用戶?我應該使用try catch塊,然後在rowCount == 0 if語句中引發異常,或者只是在將$ user變量設置爲對象時返回false並使用if語句?
我覺得返回false就夠了,跟蹤任何其他方法,讓我們等待答案 – nu6A 2012-04-27 09:28:29