-4

sorry for the noobish question. I'm new with PHP class programming and I can't figure out why this piece of code doesn't work:

class Job {
    private $var1 = 'hi there';
    private $var2 = date('Y/m/d');
    public function foo()  { /* some code */ }
}

$job = new Job();

I get parse error parse error, expecting','' or ';'' generated by $var2.
Looks like I can't initialize a variable inside a class from a PHP function.
How can I bypass this error?
Thank in advance.

Alan Piralla
  • 1,216
  • 2
  • 11
  • 21

1 Answers1

4

Initialize it from within the constructor:

class Job {
    private $var1 = 'hi there';
    private $var2 = null;
    public function __construct() { $this->var2 = date("Y/m/d"); }

    public function foo()  { /* some code */ }
}

$job = new Job();
Mate Solymosi
  • 5,699
  • 23
  • 30