发布此问题的道歉 - 我知道他们已被问及几次回答(即https://stackoverflow.com/questions/1787561/call-to-a-member-function-on-a-non-object等),但建议的解决方案似乎不起作用为了我。这是我有:PHP:调用一个非对象的成员函数GetUserCollection()
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;
}
}
为什么正是这条线:$用户列表= $这个 - >模型 - > GetUserCollection();即使我在模型中返回它的新实例,它也会抛出一个错误。在此先感谢JJ。
'__construct',而不是'_construct' – NikiC 2013-03-13 18:42:15
问题不在于你回来了。变量$ this-> model不是类“Model”的一个实例。 – Jocelyn 2013-03-13 18:43:14