PHP: alternate associative array notation not allowed inside class? -
in php, associative array notation works outside of class:
$array['a'] = array('a', 'b', 'c', 'd'); $array['b'] = array('1', '2', '3', '4');
but inside class, similar notation causes 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 works fine:
class foo { protected $array = array('a'=>array('a', 'b', 'c', 'd'), 'b'=>array('1', '2', '3', '4')); }
any idea what's going on? allowed notation can cumbersome bigger arrays.
$array['a'] = array('a', 'b', 'c', 'd'); $array['b'] = array('1', '2', '3', '4');
this means $array var defined in first line, in second put stuff it. why won't work in class, cannot define same variable twice.
even more, []=
modifying operator, can not used in class definition, same reason can not use ++
sign. not deep programming or computer inability that, design decision not logic outside of methods inside class (as opposed js or ruby example).
of course, behaviour can changed "small" c hacking of engine ;-)
Comments
Post a Comment