This question already has an answer here:
While working with an OOP script in PHP upon parsing I get a syntax error I know shouldn't be there. The syntax is perfect.
class Organism
{
private $ex = array(0=>"Hello",1=>"world!");
public $ex2 = array_rand($ex,1);
}
Gives me the error of
Parse error: syntax error, unexpected '(', expecting ',' or ';'
</div>
In PHP you must implement constructor and assign default values for your variables there.
private $ex = array(0=>"Hello",1=>"world!"); // Will work (not a function/not dynamic)
public $ex2 = array_rand($ex,1); //A function call won't work
Solution:
class Organism
{
private $ex = array(0=>"Hello",1=>"world!");
public $ex2 ;
public function __construct(){
$this->ex2 = array_rand($this->ex,1);
}
}