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.

3
  • please provide the whole class. Commented Jan 5, 2012 at 0:47
  • stackoverflow.com/questions/1499862/… Commented Jan 5, 2012 at 0:47
  • You can't use variables when defining properties on a class. Everything provided in a property definition needs to be (constant as opposed to dynamic). Commented Jan 5, 2012 at 1:00

2 Answers 2

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())
    ); 
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

class Foo {
    function __construct () {
        $this->arrays['list'] = $this->array_list ();
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.