2013-02-09 72 views
0

我想使用一個簡單的變量,在類中創建一個角色...但是,這不起作用。PHP類中的GLOBAL變量

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

回答

3

使用$GLOBALS[...]訪問全局變量是正確的。但是,在將數組訪問器嵌入到字符串中時,需要將變量包裝在括號中。

所以,與其

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

,你可以使用下列之一:

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

或:

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