我有一個DB類,我創建了幾個函數來返回不同的值。其中一個函數返回(或應該是)代表應用程序登錄用戶的「用戶」類對象。php函數不返回類對象
class user {
public $guid = '';
public $fname = '';
public $lname = '';
public function __construct() {}
public function print_option() {
return "<option value='$this->guid'>$this->name</option>";
}
}
在DB類,我有以下兩個功能:
public function get_user($uid) {
$sql = '';
$usr = new user();
$sql = "SELECT guid, fname, lname FROM ms.users WHERE guid=?";
if($sth = $this->conn->prepare($sql)) {
$sth->bind_param('s', $uid);
if($sth->execute()) {
$sth->bind_result($usr->guid, $usr->fname, $usr->lname);
$sth->fetch();
print_r($usr); // PRINTS OUT CORRECTLY
return $usr;
}
else {return null;}
}
else {return null;}
}
public function get_practice_user_list($pguid) {
$ret = '';
$sql = "SELECT user_guid FROM ms.perm WHERE p_guid=?";
if($sth = $this->conn->prepare($sql)) {
$sth->bind_param('s', $pguid);
if($sth->execute()) {
$usr = new user();
$guid = '';
$sth->bind_result($guid);
while($sth->fetch()) {
print_r($guid); // PRINTS GUID correctly
$usr = $this->get_user($guid);
print_r($usr); // PRINTS NOTHING object is null so prints "error" two lines later.
if($usr != null) $ret .= $usr->print_option();
else print "error";
}
return $ret;
}
else {return null;}
}
else {return null;}
}
我只是不理解爲什麼「用戶」的對象是不是在這種情況下返回。其他調用get_user函數的工作很好,並返回與該用戶有關的用戶類對象。
TIA
你在調用哪個函數,返回的是什麼? – ashes999 2013-05-10 01:24:41
我以get_practice_user_list函數開始。現在它什麼都不返回 – Slinky33 2013-05-10 01:27:17
它必須返回一些東西,即使那是'null'。 – 2013-05-10 01:37:44