Search code examples
phpmysqlarrayssmarty

Parse error: syntax error, unexpected '(', expecting ',' or ';' Smarty PHP


Hi everyone I have a little problem here!

I have declared an array

class CommonArray {
    public static $foundedyear_array = array_combine(range(date("Y"), 1944), range(date("Y"), 1944)); 

}

At Controller I call it out like this

$FoundedYearArr = CommonArray::$foundedyear_array;
$this->view->assign("FoundedYearArr", $FoundedYearArr );

Then it bugs Parse error: syntax error, unexpected '(', expecting ',' or ';' in

public static $foundedyear_array = array_combine(range(date("Y"), 1944), range(date("Y"), 1944)); 

How can I fix that to get the array out? Thank you everyone!


Solution

  • The docs for object property syntax read:

    declaration may include an initialization, but this initialization must be a constant value

    Which means you cannot use functions to initialize class properties. Change it to a function:

    class CommonArray {
        public static function foundedyear_array() {
            return array_combine(range(date("Y"), 1944), range(date("Y"), 1944));
        } 
    }
    

    Then call it as

    $FoundedYearArr = CommonArray::foundedyear_array();