2

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

capede
  • 945
  • 1
  • 13
  • 26
  • 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 Answers4

4

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.

Community
  • 1
  • 1
Geoff Adams
  • 1,121
  • 6
  • 15
0

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.

Josh
  • 8,082
  • 5
  • 43
  • 41
jeremyharris
  • 7,884
  • 22
  • 31
0

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"');
matino
  • 17,199
  • 8
  • 49
  • 58
0

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);
Bartosz Kowalczyk
  • 1,479
  • 2
  • 18
  • 34