1

I have an "game.php" with the following code:

<?php
include("database.php");

class Game {
    var $gameinfo;
    var $gameid;
    var $players;

    function __construct($gameinfo) {
        $this->gameinfo = $gameinfo;
        $this->gameid = $gameinfo["gameid"];

        $this->players = $database->getUserInfosByGameID($this->gameid);
    ...

and an "database.php" with the following code:

<?php

include("constants.php");

class MySQLDB {
    ... constructor etc
    function getUserInfosByGameID($gameid) { }
}
// Create database connection
global $database;
$database  = new MySQLDB();

now when creating a new game-object it throws the error

"variable $database not defined in game.php row 12"

even though in "api.php" it works like this:

<?php
// check for POST method
if($_SERVER["REQUEST_METHOD"] != "POST")
    die();

include("include/database.php");
// get json data
$stream_data = file_get_contents('php://input');
$json = json_decode($stream_data) or die("{valid=false}");

// if session in $json try to get user object from DB
if(isset($json->session))
    $sessionuser = $database->confirmUserSession($json->session);

what am I doing wrong? I've also tried defining $database without global, which also works in "api.php".

3
  • 1
    You are using include("database.php"); in game.php and include("include/database.php"); in api.php. You sure your paths are correct? Commented Jul 9, 2016 at 17:01
  • It's because the object MySQLDB is another file. Base on the structure of your code, the object is loaded first before declaring the global variable Commented Jul 9, 2016 at 17:02
  • Charlie Fish, yes the path is correct, the game.php and database.php are in subfolder "include" Commented Jul 11, 2016 at 12:44

2 Answers 2

2

You should declare global $database in the constructor as well so the method has access to the variable.

function __construct($gameinfo) {
    global $database;
    $this->gameinfo = $gameinfo;
    $this->gameid = $gameinfo["gameid"];

    $this->players = $database->getUserInfosByGameID($this->gameid);

EDIT

Here is the official docs about this topic.

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

1 Comment

It works because a class' method is an airtight environment. So it has access only to the class' attributes or the inputs. If you want to use a global variable you have to declare it inside the method so it has visibility of that. You can read the official docs about this topic.
1

Put your global variable before MySQLDB because class MySQLDB will look for $database.

<?php

global $database;

include("constants.php");

class MySQLDB {
    ... constructor etc
    function getUserInfosByGameID($gameid) { }
}
// Create database connection
$database  = new MySQLDB();

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.