开发者

php associative array class variables

开发者 https://www.devze.com 2023-04-03 16:47 出处:网络
so I have this class class A{ public $something[\'aaa\'] = \'soemthing\'; } but then it complains that there is syntax error....

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");
}
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消