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


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

  • $this-> is only used inside the class itself (so in its methods). In this case you need to instantiate the class $foo = new test(); $foo->optList; or make it static and access via test::$optList. Have you read the docu on classes? – kero Jul 17 '18 at 6:16
  • Yes i read the docs. i know how to used method in class. i want to divided public Variables in parts as mentioned above. – Allex Jul 17 '18 at 6:22

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' )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.