调用一个非对象的成员函数query() [英] Call to a member function query() on a non-object

查看:140
本文介绍了调用一个非对象的成员函数query()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这太奇怪了!当我尝试运行下面的代码时,我正在运行PHP版本5.1.6,它给出了一个尚未实例化的对象的致命错误。只要我取消注释这行代码
// $ cb_db = new cb_db(USER,PASSWORD,NAME,HOST);
一切正常。即使我已经在方法中将$ cb_db对象声明为全局的。
$ b

require_once(ROOT_CB_CLASSES。'db.php');

  $ cb_db = new cb_db(USER,PASSWORD,NAME,HOST); 

class cb_user {
保护函数find_by_sql($ sql){
global $ cb_db;

// $ cb_db = new cb_db(USER,PASSWORD,NAME,HOST);
$ result_set = $ cb_db->查询($ sql);

$ object_array = array();
while($ row = $ cb_db-> fetch_array($ result_set)){
$ object_array [] = self :: instantiate($ row);
}
返回$ object_array;
}
}


解决方案

你将使用全局变量(而不是通过构造函数传递连接),你应该确保它是Global-ed这两个地方你打算使用它。

  Global $ cb_db; 
$ cb_db = new cb_db(USER,PASSWORD,NAME,HOST);

class cb_user {
保护函数find_by_sql($ sql){
global $ cb_db;

// $ cb_db = new cb_db(USER,PASSWORD,NAME,HOST);
$ result_set = $ cb_db->查询($ sql);

$ object_array = array();
while($ row = $ cb_db-> fetch_array($ result_set)){
$ object_array [] = self :: instantiate($ row);
}
返回$ object_array;
}
}

然而,我相信有一个更好的方法可以做到这一点通过构造函数传入数据库连接。

  $ cb_db = new cb_db(USER,PASSWORD,NAME,HOST); 
$ cb_user = new cb_user($ cb_db);

class cb_user {
public __construct(cb_db $ database)
{
$ this-> database = $ database
}

保护函数find_by_sql($ sql){

$ this-> database = new cb_db(USER,PASSWORD,NAME,HOST);
$ result_set = $ cb_db->查询($ sql);

$ object_array = array();
while($ row = $ cb_db-> fetch_array($ result_set)){
$ object_array [] = self :: instantiate($ row);
}
返回$ object_array;


$ / code $ / pre

您也可以将它传递给单个函数 find_by_sql($ sql,$ cb_db))如果您发现这是使用连接的唯一函数。


Ok, this is so weird!!! I am running PHP Version 5.1.6 when I try and run the code below it gives a fatal error of an object that has not been instantiated. As soon as I un-comment this line of code //$cb_db = new cb_db(USER, PASSWORD, NAME, HOST); everything works. Even though I have declared the $cb_db object as global within in the method. Any help would be greatly appreciated.

require_once ( ROOT_CB_CLASSES . 'db.php');

$cb_db = new cb_db(USER, PASSWORD, NAME, HOST);

class cb_user {
    protected function find_by_sql( $sql ) {
        global $cb_db;

        //$cb_db = new cb_db(USER, PASSWORD, NAME, HOST);
        $result_set = $cb_db->query( $sql );

        $object_array = array();
        while( $row = $cb_db->fetch_array( $result_set ) ) {
            $object_array[] = self::instantiate( $row );
        }
        return $object_array;
    }
}

解决方案

If you are going to use globals (instead of passing the connection through the constructor), you should make sure that it is Global-ed both places you plan to use it.

Global $cb_db;
$cb_db = new cb_db(USER, PASSWORD, NAME, HOST);

class cb_user {
    protected function find_by_sql( $sql ) {
        global $cb_db;

        //$cb_db = new cb_db(USER, PASSWORD, NAME, HOST);
        $result_set = $cb_db->query( $sql );

        $object_array = array();
        while( $row = $cb_db->fetch_array( $result_set ) ) {
            $object_array[] = self::instantiate( $row );
        }
        return $object_array;
    }
}

However, I believe a better way to do this would be by passing the database connection in through the constructor.

$cb_db = new cb_db(USER, PASSWORD, NAME, HOST);
$cb_user = new cb_user($cb_db);

class cb_user {
    public __construct(cb_db $database)
    {
        $this->database = $database
    }

    protected function find_by_sql( $sql ) {

        $this->database = new cb_db(USER, PASSWORD, NAME, HOST);
        $result_set = $cb_db->query( $sql );

        $object_array = array();
        while( $row = $cb_db->fetch_array( $result_set ) ) {
            $object_array[] = self::instantiate( $row );
        }
        return $object_array;
    }
}

You could also pass it in to the individual function (find_by_sql($sql, $cb_db)) if you find that this is the only function which uses the connection.

这篇关于调用一个非对象的成员函数query()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆