0

I'm having trouble declaring an array in conjunction to a function. Here is my code, what am I doing wrong?

private function array_list(){
    return array('1'=>'one', '2'=>'two');
}

private $arrays= array(
    'a'=>array('type'=>'1', 'list'=>$this->array_list())
);

Getting unexpected T_VARIABLE error when I run this code.

user389767
  • 73
  • 1
  • 9

2 Answers2

1

You cannot declare arrays like this as property:

private $arrays= array(
    'a'=>array('type'=>'1', 'list'=>$this->array_list())
);

You cannot use an array returned from a class method in the property definition. You should populate it inside a constructor for example. Like this:

private $arrays = array();

public function __construct() {
    $this->arrays = array(
        'a'=>array('type'=>'1', 'list'=>$this->array_list())
    ); 
}
Shoe
  • 74,840
  • 36
  • 166
  • 272
0

Do it in a method, for example, the constructor:

class Foo {
    function __construct () {
        $this->arrays['list'] = $this->array_list ();
    }
}
mishmash
  • 4,422
  • 3
  • 34
  • 56