0

我想使用一个在班级中发挥作用的简单变量......但是,这不起作用。

$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);
4

1 回答 1

3

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

所以,而不是

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

您可以使用以下方法之一:

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

或者:

global $world;
file_get_contents("... $world");
于 2013-02-09T16:26:08.500 回答