很抱歉发布这个问题 - 我知道他们已经被问过并回答了好几次(即https://stackoverflow.com/questions/1787561/call-to-a-member-function-on-a-non-object等) ,但建议的解决方案似乎都不适合我。这就是我所拥有的:
class Common {
private $model;
public function _construct() {
$this->model = Model::GetInstance();
//$this->model->GetUserCollection();
}
public function validateusername($username) {
$userlist = $this->model->GetUserCollection();
$result = true;
if ($username == false) {
$this->error = "Please enter username.";
$result = false;
}
}
}
// This is my DB model
class Model {
private static $instance = null;
private $conn = false;
private function __construct() {
if (!$this->connect()) {
print "Unable to connect to database!";
exit;
}
}
public function connect() {
if ($this->conn == false) {
$this->conn = mysql_connect(DBHOST, DBUSER, DBPASS);
if ($this->conn) {
if (!mysql_select_db(DBNAME, $this->conn)) {
print "DB Connection Error!";
exit;
}
}
}
return $this->conn;
}
public function GetUserCollection() {
return UserCollection::GetInstance();
// return new UserCollection();
}
public static function GetInstance() {
if (!self::$instance) {
self::$instance = new Model();
}
return self::$instance;
}
}
为什么正是这一行:$userlist = $this->model->GetUserCollection(); 即使我在模型中返回它的新实例,这也会给我一个错误提前谢谢,JJ。