Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i have these in Database.php

class Database
{
    private $dbname = 'auth';
    private $db_instance;   

    public function __construct($q)
    {
        if(!$this->db_instance)
        $this->db_instance = new SQLite3($q);
    }

    private function init()
    {
        if(!$this->db_instance)
        {
            $this->db_instance = new SQLite3($dbname);
        }
    }

    private function close()
    {
        if($this->db_instance)
        $this->db_instance->close();
    }

    static public function fetch_a_row($q)
    {
        $this->init();
        $res = $this->db_instance->query(SQLite3::escapeString($q));
        $ret = $res->fetchArray(SQLITE3_ASSOC); 
        $this->close();

        return $ret;
    }

    static public function exec($q)
    {
        $this->init();
        $this->db_instance->exec(SQLite3::escapeString($q));
        $this->close();
    }


}

in try to call him on index.php

<?php
require_once('Database.php');

$ret = Database::fetch_a_row('SELECT * FROM user WHERE uname = "test"');

echo $ret['id'];
?>

but response :

Fatal error: Using $this when not in object context in /www/cgi-bin/auth/Database.php on line 29

is there anyone could help ? thank in advance

share|improve this question
Friendly advice: If this class is not purely for educational purposes just drop it and use slightly more advanced, existing classes/frameworks. If it is for educational purposes only, drop it anyway and start by studying other database libraries ;-) – VolkerK Mar 22 '12 at 14:12

4 Answers

up vote 3 down vote accepted

In your static method, you are using the instance accessor $this-> which is not valid syntax. Typically, you would need to use self::, but it isn't as simple as that. Take a look at this question, it should explain the difference between using self (when in static methods) and $this (when in non-static methods).

From a glance at the code though, the Database class requires you to instantiate the object in order to create the DB instance utilised later on. For this reason, it would make more sense to make your query methods fetch_a_row and exec non-static, and change your code to instantiate the object and use that instance.

However, this is all stuff that you must figure out as the Database class needs a bit of refactoring in order to work. The first thing to ask yourself would be "why are you utilising static methods?" - if you don't need them, it may be simpler to use non-statics (as mentioned above). From there, I guess it's up to you the direction you take.

share|improve this answer

You can't use this in static context. Remove static from the function and call it like this:

$db = new Database();
$ret = $db->fetch_a_row('SELECT * FROM user WHERE uname = "test"');
share|improve this answer

You must create an instance of the database class.

$db = new Datatbase('test');

Now you have object where you can use

$results= $db->fetch_a_row(query);
share|improve this answer

You are using $this in a static function, for which there is no context (because static refers to the class whereas $this looks for the current object). Try self in your class' fetch_a_row function instead.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.