so I have this class
class A{
public $something['aaa'] = 'soemthing';
}
but then it complains that there is syntax error....
how ca开发者_C百科n I set class variables in PHP as an associative array?
Can't say I'm right saying this.. but you might have to declare it in the constructor:
class A{
public $something; // or $something = array();
function __construct($something){
$this->something['aaa'] = $something;
}
}
That's strange. I don't think that's invalid syntax but it is throwing an error on my end. Maybe the parsre just isn't equipped to handle an property being initialized in that way. When I tried the following equivalent initialization it seemed to work just fine:
<?php
class A {
public $something = array("aaa" => "something");
}
?>
精彩评论