Greetings good people,
I am running with some issue with OOP recently. First, I am looking to create CRUD Db class from scratch. This is a code:
class Database{
public static $link;
public $message;
public function __construct () {
try {
self::$link = mysqli_connect(HOST, USER, PASS, DB);
if (!self::$link) {
echo self::$link->error;
}
} catch (Exception $e) {
die(" Failed connecting to DB") ;
}
}
public static function query($SQL){
return self::$link->query($SQL);
}
public static function select($table, array $columns){
$columns=implode(",",$columns);
$result=self::query("SELECT $columns FROM $table");
return self::$link->fetch($result);
}
}
So the problem is this:
If I call my static select function like this:
Database::select('users', array('username'=>'user'));
It returns: Fatal error: Call to a member function query() on a non-object in ..
And if I debug connection like this: var_dump(Database::$link)
it returns NULL
But if I place "$db = new Database();" above that line, it works?
Can anyone instruct me what I am doing wrong?
Thanks!