访问类中数组中的变量

时间:2015-04-09 12:42:30

标签: php class

我如何解决这个问题。我想访问数组中的变量

class testClass{
   public $variable;
   public function __construct(){
       $this->variable = 3; //assign value to variable
   }

   public $arr = array(
       'index' => $this->variable//here show the error
   )
}

6 个答案:

答案 0 :(得分:0)

问题是您无法初始化类似的类属性。根据{{​​3}}:

  

这个声明可能包括初始化,但是这个   初始化必须是一个常量值 - 也就是说,它必须能够   在编译时进行评估,不得依赖于运行时   信息以便进行评估。

所以你需要把它放在构造函数中:

public $arr = array(
   'index' => $this->variable//here show the error
)

就像:

public function __construct(){
   $this->variable = 3; //assign value to variable

   // here you can assign your variable using another property
   $this->arr = array(
      'index' => $this->variable//here show the error
   );
}

答案 1 :(得分:0)

嗯,你不能在default值中做到这一点;

你有2个选择; 一,通过构造设置它:

class testClass{
    public $variable;
    public $arr;

    public function __construct(){
        $this->variable = 3;
        $this->arr = array('index' => $this->variable);
    }
}

二,使用功能;

class testClass{
    public $variable;

    public function __construct(){
        $this->variable = 3;
    }

    public function arr()
    {
        return array(
            'index' => $this->variable,
        );
    }
}

答案 2 :(得分:0)

class testClass
{
   protected $arr = array();
   protected $variable; // define this class variable only if you use it in another method

   public function __construct()
   {
       $this->variable = 3;
       $this->setArrElements(array('index' => $this->variable));
   }

   public function doSomething()
   {
       echo $this->arr['index'];
   }

   public function getArr()
   {
       return $this->arr;
   }

   public function addArrElements(Array $elements)
   {
       $this->arr = array_merge($this->arr, $elements);
   }
}

$test = new testClass();
$test->doSomething();

答案 3 :(得分:0)

你应该制作一个getter和setter方法。

class testClass{
   public $variable;
   public $arr;

   public function __construct(){
       $this->variable = 3; //assign value to variable
       $this->arr = array('index' => $this->variable);
   }

   public function getArr($key) {
      return $this->arr[$key];
   }

   public function setArr($key, $val) {
      return $this->arr[$key] = $val;
   }
}

$testClass = new testClass();

print $testClass->getArr('index');

答案 4 :(得分:0)

class testClass{
   public $variable;
   public $arr = array();

   public function __construct(){
      $this->variable = 3; //assign value to variable
      $this->arr["index"] = $this->variable;
   }

}

$test = new testClass();
echo $test->arr["index"]; // shows the value off index;

这是在数组中添加和访问值的正确方法。

答案 5 :(得分:0)

如前所述

  

这个声明可能包括初始化,但是这个   初始化必须是一个常量值 - 也就是说,它必须能够   在编译时进行评估,不得依赖于运行时   信息以便进行评估。

提供不同的观点/答案。您可以/应该使用常量来定义默认值,以便在扩展对象时可以轻松地引用/覆盖它们,而不是需要覆盖实现该值的每个对象/变量。

class testClass
{

    const DEFAULT_INDEX = 3;

    public $variable = self::DEFAULT_INDEX;

    public $arr = array(
       'index' => self::DEFAULT_INDEX
    );
}

http://ideone.com/o5EavE

如果您希望始终将$this->variable作为testClass::$arr['index']引用,我建议您使用下面提供的setter / getter。