2

In PHP, this associative array notation works outside of a class:

$array['a'] = array('a', 'b', 'c', 'd');
$array['b'] = array('1', '2', '3', '4');

But inside a class, similar notation causes an error:

class Foo {
    protected $array['a'] = array('a', 'b', 'c', 'd');
    protected $array['b'] = array('1', '2', '3', '4');
}

//Parse error: syntax error, unexpected '[', expecting ',' or ';'

And yet this works just fine:

class Foo {
    protected $array = array('a'=>array('a', 'b', 'c', 'd'), 'b'=>array('1', '2', '3', '4'));
}

Any idea what's going on? The allowed notation can get really cumbersome with bigger arrays.

2
  • What's wrong with splitting the last definition into separate lines, one per sub-array? Commented Jul 2, 2011 at 7:10
  • Anyway, you're probably confusing class properties/fields with local variables. They are two entirely different things. You can't say $array['a'] and $array['b'] are distinct protected properties of instances of Foo, because they are actually offsets of a single array called $array. Commented Jul 2, 2011 at 7:11

4 Answers 4

5
$array['a'] = array('a', 'b', 'c', 'd');
$array['b'] = array('1', '2', '3', '4');

this means the $array var was defined in the first line, in the second you only put stuff into it. That is why it won't work in a class, you cannot define the same variable twice.

Even more, the []= is a modifying operator, which can not be used in class definition, the same reason you can not use the ++ sign. Not a deep programming or computer inability to do that, just a design decision not to do logic outside of methods inside a class (As opposed to JS or Ruby for example).

Of course, all that behaviour can be changed by "small" C hacking of the engine ;-)

Sign up to request clarification or add additional context in comments.

Comments

0

I don't know the technical reason why you can't do like you describe. Probably something silly and very low level. You could easily get around this, for a concrete class anyway, by setting the arrays in the constructor like this:

class foo {

protected $bar = new array();

  function __construct() {
    $array['a'] = array('a', 'b', 'c', 'd');
    $array['b'] = array('1', '2', '3', '4');

    $this->bar = $array;
  }

}

1 Comment

The technical reason is simply that that's the wrong place to modify an existing variable. The right place is in, as you say, the constructor.
0

Don't know exact reason. Suppose php still has some kinks in its code.

class Foo {
    protected $array=array();
    public function Foo()
    {
    $array['b'] = array('1', '2', '3', '4');
    }
}

But this compiles ok. So you can just put the code in the constructor.

1 Comment

No, it doesn't. It's the expected behavior.
0

Something wrong with

public function __construct() {
   $this->array['a'] = array('a', 'b', 'c', 'd');
   $this->array['b'] = array('1', '2', '3', '4');
}

I can only postulate on the technical reasons for this. A guess is that [] is an operation in php which can change the size in memory of a variable. Class definitions should have a constant space in memory when initialized.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.