Why is this code giving me an error?

<?
class Events
{
public $year = date("Y")+1;

public function sql() {

	$sql = "select * from eventDates where eventDate < '" . $this->year . "' order by id";
return $sql;
}

}

$c = new Events();
echo $c->sql();
?>

The above gives the error:

PHP Parse error: syntax error, unexpected '(', expecting ',' or ';' in test2.php on line 5

I need to dynamically generate $year. How do I do that within the class?

    Class variable declarations can only be assigned literal values; you cannot assign them variables or the results of functions (such as date()). The likely solution is to do the assignment in the constructor:

    <?
    class Events
    {
       public $year;
       public function __construct()
       {
          $this->year = date("Y") + 1;
       }
    
      Write a Reply...