dongxian8858 2013-03-13 18:37
浏览 8
已采纳

PHP:在非对象上调用成员函数GetUserCollection()[关闭]

Apologies for posting this question - I know they have been asked and answered several times (i.e. https://stackoverflow.com/questions/1787561/call-to-a-member-function-on-a-non-object, etc), but non of the suggested solutions seem to work for me. This is what I have:

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;
    }
}

Why is it exactly this line : $userlist = $this->model->GetUserCollection(); which is throwing me an error even though I am returning a new instance of it in the in the model Thanks in advance, JJ.

  • 写回答

1条回答 默认 最新

  • dream6120 2013-03-13 18:44
    关注

    Use two underscores in your constructor name: __construct.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求