Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP类:在构造函数之后取消初始化数组?_Php_Arrays_Class_Constructor_Initialization - Fatal编程技术网

PHP类:在构造函数之后取消初始化数组?

PHP类:在构造函数之后取消初始化数组?,php,arrays,class,constructor,initialization,Php,Arrays,Class,Constructor,Initialization,我正在用php创建一个类,但是我对其中一个类变量有一些问题。我声明一个私有变量,然后在构造函数中设置它。但是,在类的后面,我有一个使用该变量的方法。本例中的变量是数组。然而,这个方法说数组是空的,但是当我在构造函数中检查它时,一切正常。所以真正的问题是,为什么我的数组在构造函数之后是清除的,或者看起来是清除的 <?php class Module extends RestModule { private $game; private $gamearray; pub

我正在用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];
    }
}
您有一个输入错误:

public function formSelect(){
    $gamename = Utility::get('game');
    $this->gamearray[$gamename]; // Remove the $ before gamearray
}
此外,在您的情况下,
include
优于
require\u once

如果你想更深入,你可以像这样重写
$gamearray
作业:

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

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

我应该上床睡觉再看一眼,这样我就不会那么傻了,哦,非常感谢!我也学到了包含的东西,所以非常感谢!!!