PHP "global" 无法在 __construct 中访问变量
PHP "global" variable not accessible in __construct
我有一个 "game.php",代码如下:
<?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);
...
和一个 "database.php",代码如下:
<?php
include("constants.php");
class MySQLDB {
... constructor etc
function getUserInfosByGameID($gameid) { }
}
// Create database connection
global $database;
$database = new MySQLDB();
现在创建新游戏对象时会抛出错误
"variable $database not defined in game.php row 12"
尽管在 "api.php" 中它是这样工作的:
<?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);
我做错了什么?我也试过在没有全局的情况下定义 $database,它也适用于 "api.php".
您还应该在构造函数中声明全局 $database
,以便该方法可以访问变量。
function __construct($gameinfo) {
global $database;
$this->gameinfo = $gameinfo;
$this->gameid = $gameinfo["gameid"];
$this->players = $database->getUserInfosByGameID($this->gameid);
编辑
这是关于这个话题的官方 docs。
将全局变量放在 MySQLDB
之前,因为 class MySQLDB
会查找 $database
.
<?php
global $database;
include("constants.php");
class MySQLDB {
... constructor etc
function getUserInfosByGameID($gameid) { }
}
// Create database connection
$database = new MySQLDB();
我有一个 "game.php",代码如下:
<?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);
...
和一个 "database.php",代码如下:
<?php
include("constants.php");
class MySQLDB {
... constructor etc
function getUserInfosByGameID($gameid) { }
}
// Create database connection
global $database;
$database = new MySQLDB();
现在创建新游戏对象时会抛出错误
"variable $database not defined in game.php row 12"
尽管在 "api.php" 中它是这样工作的:
<?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);
我做错了什么?我也试过在没有全局的情况下定义 $database,它也适用于 "api.php".
您还应该在构造函数中声明全局 $database
,以便该方法可以访问变量。
function __construct($gameinfo) {
global $database;
$this->gameinfo = $gameinfo;
$this->gameid = $gameinfo["gameid"];
$this->players = $database->getUserInfosByGameID($this->gameid);
编辑
这是关于这个话题的官方 docs。
将全局变量放在 MySQLDB
之前,因为 class MySQLDB
会查找 $database
.
<?php
global $database;
include("constants.php");
class MySQLDB {
... constructor etc
function getUserInfosByGameID($gameid) { }
}
// Create database connection
$database = new MySQLDB();