I just got started learning OOP in PHP, so forgive me if this is actually a very trivial question.
I have a class called Engine, and I have the class database, which extends engine. Some classes I call in scripts also extend engine. Now I want to make it so that I dont have to set the instance of $db to global in every method of other classes I have.
I thought the solution would be to make the MySqlDatabase class static, but somehow I get errors. Could someone tell me what I am doing wrong?
It's probably a very simple thing but im kind of stuck at the moment.
Thanks in advance!
<?php
class MySqlDatabase extends engine{
public static $connection;
public static $query;
function __construct(){
MySqlDatabase::open_connection();
}
public static function open_connection(){
self::$connection=mysqli_connect("localhost","XXX","XXX","XXX");
mysqli_set_charset(self::$connection,'utf8') or die("Charset error");
}
public static function close_connection(){
mysqli_close(self::$connection);
}
public static function con(){
return self::$connection;
}
public static function query($query){
$result = mysqli_query(self::$connection,$query);
self::$query = $result;
self::confirm_query($result);
return $result;
}
public static function confirm_query($result){
if(!$result){
die("DB Query failed:".mysqli_error(self::$connection));
}
}
public static function mysql_prep($string) {
$escaped_string = mysqli_real_escape_string(self::$connection,$string);
return $escaped_string;
}
public static function num_rows(){
$result = mysqli_num_rows(self::$query);
return $result;
}
public static function fetch_array(){
$result = mysqli_fetch_array(self::$query);
return $result;
}
}
?>
Now when I want to get something from the DB I get errors when I use: '; ?>
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/devogdata/public_html/includes/dbconnect.php on line 39
Warning: mysqli_error() expects parameter 1 to be mysqli, null given in /home/devogdata/public_html/includes/dbconnect.php on line 47 DB Query failed:
__construct()function is not used