-1

I have a application class which is extended from base class named db. When i try to create an instance, I get error as

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

My Application Class is:

Class Application extends DB
{

public $sql;

 /*
  * Public Methods
 */
/*
 * Constructor
 */
function Application() {
    global $sql;

    $sql = db::getInstance();
}

function selectSqli($access_table){
    $result = $sql->query("select * from $access_table");
    return $result;
}

}

My DB Class is:

class db extends mysqli{
protected static $instance;
protected static $options = array();

private function __construct() {
    $db = self::$options;

    // turn of error reporting
    mysqli_report(MYSQLI_REPORT_OFF);

    // connect to database
    @parent::__construct(isset($db['host'])   ? $db['host']   : 'localhost',
                         isset($db['user'])   ? $db['user']   : 'root',
                         isset($db['pass'])   ? $db['pass']   : '',
                         isset($db['dbname']) ? $db['dbname'] : 'angler_blog_post');

    // check if a connection established
    if( mysqli_connect_errno() ) {
        throw new exception(mysqli_connect_error(), mysqli_connect_errno()); 
    }
}

public static function getInstance() {
    if( !self::$instance ) {
        self::$instance = new self(); 
    }
    return self::$instance;
}

public function query($query) {
    if( !$this->real_query($query) ) {
        throw new exception( $this->error, $this->errno );
    }

    $result = new mysqli_result($this);
    return $result;
}
}

I tried global, plublic but i cannot access the function query(). How to solve this bug.

hakre
  • 193,403
  • 52
  • 435
  • 836
Raju.allen
  • 341
  • 2
  • 9
  • 21
  • I believe you forgot to add a `global $sql;` in your selectSqli function. – Miklos Aubert Mar 28 '13 at 20:09
  • Scratch that, why do you have a `global $sql` in your Application constructor? Shouldn't you be using `$this->sql`? – Miklos Aubert Mar 28 '13 at 20:12
  • Are you aware that `extends` defines an `is a` relationship. Which means that you have written "every Application is a Database" statement in your code. That's kinda really stupid. – tereško May 04 '14 at 11:48

2 Answers2

1

You should better inject your DB Object than using global $sql;

But in your case, $sql is a local variable, not property. And as you declared in the constructor $this->sql: property $sql of current object now contains an instance of DB object.

So, it should be:

function selectSqli($access_table){
    $result = $this->sql->query("select * from $access_table");
    return $result;
}

And also, you probably don't know some news about PHP 5, because using method name which is similar to class name for achieving constructor, in PHP 5 you should use a special method __construct() {} which is for constructor.

See what is new in PHP 5 in OOP: http://www.php.net/manual/en/language.oop5.php

Lkopo
  • 4,798
  • 8
  • 35
  • 60
0
function selectSqli($access_table){
    global $sql;
    $result = $sql->query("select * from $access_table");
    return $result;
}

Or if you use OOP:

Class Application extends DB
{

public $sql;

 /*
  * Public Methods
 */
/*
 * Constructor
 */
function Application() {
    $this->sql = db::getInstance();
}

function selectSqli($access_table){
    $result = $this->sql->query("select * from $access_table");
    return $result;
}
}
Alexey Palamar
  • 1,440
  • 1
  • 10
  • 16