-1

Whenever I'm trying to call $this->dbc in Is_Staff it returns an error saying:

Fatal error: Using $this when not in object context

And I'm not quite sure why it's doing this. I have tried making $dbc global but that doesn't seem to do anything.

Class BoardDatabase {

            public $dbhost = "localhost"; // Host
            public $dbuser = "root"; // Username
            public $dbpass = ""; // Password
            public $dbname = "NecBoard"; // Database

            public function __construct($dbhost, $dbuser, $dbpass, $dbname) {

                $this->dbc = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname) or die("Couldnt connect to the database! " . mysqli_errno);
                if(!$this->dbc) {
                    die("Could not connect to database: " . $this->dbc->connect_errno());
                }
            }

            public static function Is_Staff($type, $bool) {

                if($type == "mod") {
                    if($bool == true) {

                        $IsStaff = $this->dbc->prepare("SELECT is_mod FROM users WHERE id = ?");
                        $IsStaff->bind_param("s", $_SESSION["id"]);
                        $IsStaff->execute();
                        $IsStaff->bind_result($is_mod);

                        $IsStaff->store_result();
                        if($IsStaff->num_rows >= 1) {

                            $IsStaff->fetch();
                            if($is_mod >= 1) {
                                return true;
                            } else {
                                return false;
                            }

                        }

                    } else {
                        return false;
                    }
                }

            }

        }
P. Nick
  • 955
  • 1
  • 12
  • 33
  • You did not declare dbc in this context yet . if you want to use dbc object from another class then first load the class and then create object – Manoz Biswas Apr 11 '16 at 16:03
  • http://stackoverflow.com/questions/15735099/fatal-error-using-this-when-not-in-object-context-in – daxro Apr 11 '16 at 16:03
  • Why do you have a static method? – PeeHaa Apr 11 '16 at 16:06
  • The code where you reference the object (that is, the calling code) would be also relevant. My guess would be that you use your class as a static class. Inside a static class `$this->` does not work; use `self::$` instead. However, mixing static-and non-static behaviour is not a good coding practice, and in some cases PHP would through notices or warnings about this. – syck Apr 11 '16 at 16:15

1 Answers1

2

Problems

Firstly, you are accessing the $dbc variable as a context of the object BoardDatabase when it is not even a property of the object itself.

Secondly, you cannot access a variable using the $this in a static context.

Solution

Declare the variable $dbc at the top as a static variable:

public static $dbc;

And access it this way in the object context:

self::$dbc

This should solve your problem.

awoyotoyin
  • 792
  • 4
  • 8