PHP Use Declared array Variable inside already Declared Array

i have created one class in which i declared public associative array.

class test{
public $basicCols= array(
      array('title'='KEY', 'field'='slug','options'=$optList),
      array('title'='KEY', 'field'='slug'),
}

public $optList= array("one"="One","two"="Two");

}

But when i execute code its giving me error. I tried

$this-$optList
$this-optList
$optlist

is there any other way to declare variable inside variable in class. thank you in advance.

Topic array variables php post-class Wordpress

Category Web


This is more a generic PHP question than anything to do with WordPress, but I'd suggest setting the value in the constructor:

class test {
    public $basicCols;

    public $optList = array( 'one' => 'One', 'two' => 'Two' );

    function __construct() {
        $this->basicCols = array(
            array( 'title' => 'KEY', 'field' => 'slug', 'options' => $this->optList ),
            array( 'title' => 'KEY', 'field' => 'slug' )
        );
    }
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.