2012-07-11 56 views
2

我在php中做了一个类,但是Im在类变量中遇到了一些问题。我声明一个私有变量,然后在构造函数中设置它。然而,在课程的后面,我有一个使用该变量的方法。这种情况下的变量是一个数组。然而,该方法说数组是空白的,但是当我在构造函数中检查它时,它一切正常。所以真正的问题是,为什么我的数组在构造函数之后清晰或似乎清晰?PHP类:数组在构造函数后被初始化?

<?php 
class Module extends RestModule { 
    private $game; 
    private $gamearray; 

    public function __construct() { 
     require_once (LIB_DIR."arrays/gamearray.php"); 
     $this->gamearray = $gamesarray; 
     $this->game = new Game(); 
     $this->logger = Logger::getLogger(__CLASS__); 
     $this->registerMethod('add', array(Rest::AUTH_PUBLIC, Rest::AUTH_USER, Rest::AUTH_ADMIN), true); 
     $this->registerMethod('formSelect', array(Rest::AUTH_PUBLIC, Rest::AUTH_USER, Rest::AUTH_ADMIN), false); 
    } 

    public function add(){ 
     $game = Utility::post('game');   
    } 

    public function formSelect(){ 
     $gamename = Utility::get('game'); 
     $this->$gamearray[$gamename]; 
    } 
} 

该数组从另一个文件中拉入,因为该数组包含大量文本。不想用构造函数中声明的巨大数组混搭这个文件。滚动将是巨大的。任何解释都会很好,我喜欢理解我的问题,而不仅仅是解决问题。

回答

1

你有一个错字:

public function formSelect(){ 
    $gamename = Utility::get('game'); 
    $this->gamearray[$gamename]; // Remove the $ before gamearray 
} 

而且,你的情况,includerequire_once更好。

如果要深究下去,你可以重写$gamearray分配是这样的:

// Module.php 
$this->gamearray = include LIB_DIR.'arrays/gamearray.php'; 

// gamearray.php 
return array(
    // Your data here 
); 
+0

应该去睡觉,它relooked所以我会感觉不那么愚蠢,哦也非常感谢!我也学会了包括东西,所以非常感谢! – samuraiseoul 2012-07-11 15:23:31

+0

使用'error_reporting(E_ALL)'会提出通知:) – Florent 2012-07-11 15:26:46