Call to a member function query() on a non-object

2019-08-21 16:06发布

问题:

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

回答1:

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.