【问题标题】:access variable in a array in class [duplicate]访问类中数组中的变量[重复]
【发布时间】:2015-06-14 20:06:15
【问题描述】:

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

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

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

【问题讨论】:

  • 请发布您遇到的错误

标签: php class


【解决方案1】:

问题是你不能像这样初始化一个类属性。根据manual

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

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

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

【讨论】:

  • 似乎是这些问题的日子;我们今天已经有一个类似的:stackoverflow.com/q/29538287/3933332 :) (另外,如果 OP 没有在定义中定义属性 $arr,它将自动公开)
  • @Rizier123 老实说,它肯定是重复的,但我找不到足够快的,写一个答案更容易。并且该主题必须在今天的课程中;-)
  • 这一切都是为了知道是否存在欺骗。但是这个Q还有其他一些方面。就像属性在构造函数中定义时的可见性一样。
  • @Rizier123 OP 实际上正在定义它。在构造函数之后,但仍然。而且也是公​​开的,所以我认为这不是一个真正的问题。它实际上与您在另一个问题中链接的问题完全相同,不同之处在于属性定义之间存在构造函数:-)
【解决方案2】:

你不能在 default 值中这样做;

你有两个选择; 一、通过构造设置:

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,
        );
    }
}

【讨论】:

    【解决方案3】:
    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();
    

    【讨论】:

      【解决方案4】:

      你应该创建一个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');
      

      【讨论】:

        【解决方案5】:
        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;
        

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

        【讨论】:

          【解决方案6】:

          如前所述

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

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

          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。

          【讨论】:

            猜你喜欢
            • 2019-12-05
            • 1970-01-01
            • 2018-08-17
            • 1970-01-01
            • 2013-02-18
            • 1970-01-01
            • 1970-01-01
            • 2013-11-05
            • 1970-01-01
            相关资源
            最近更新 更多