PHP类中的GLOBAL变量


GLOBAL variable in PHP class

我想使用一个简单的变量来在类中扮演角色。。。然而,这并不奏效。

$GLOBALS['world'] = "Isara";
class Character{
    var $name;
    var $status;
    static $content;
    function __construct($name){
        $this->name=$name;
        $this->getCharInfo();
    }
    private function getCharInfo(){
        if(empty(self::$content)){
            self::$content = file_get_contents("http://www.tibia.com/community/?subtopic=worlds&world=$GLOBALS['world']",0);

使用$GLOBALS[...]访问全局变量是正确的。但是,在字符串中嵌入数组访问器时,需要将变量包装在括号中。

所以,不是

file_get_contents("... $GLOBALS['world']");

您可以使用以下选项之一:

file_get_contents("... {$GLOBALS['world']}");
file_get_contents("... " . $GLOBALS['world']);

或:

global $world;
file_get_contents("... $world");