1

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:

1
  • 1
    when you call a static method, the __construct() function is not used Commented Mar 10, 2016 at 14:27

1 Answer 1

3

Constructors are only called when an object is created, when you are calling a static method you call it from the class itself like this className::static_method() and not from the object. You will have to manually call the static method open_connection first and then the query so that it will instantiate the connection variable.

   MySqlDatabase::open_connection();
   MySqlDatabase::query('your sql query');

More information on static keyword and methods : http://php.net/manual/en/language.oop5.static.php

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.