Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php associative array class variables

so I have this class

class A{

  public $something['aaa'] = 'soemthing';

}

but then it complains that there is syntax error....

how can I set class variables in PHP as an associative array?

like image 681
kamikaze_pilot Avatar asked Mar 26 '26 00:03

kamikaze_pilot


2 Answers

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;
  }

}
like image 132
Atticus Avatar answered Mar 28 '26 17:03

Atticus


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");
}
?>
like image 27
Joe Landsman Avatar answered Mar 28 '26 16:03

Joe Landsman