Setting date to a variable in a PHP class
date, datetime, php
Solution
You cannot use a function call or a variable outside of a function inside of a class. The default values must be static.
The following assignments are valid:
public $data = "hello";
public $data = array(1,2,3,4);
public $data = SOME_CONSTANT;
The following are not:
public $data = somefunction();
public $data = $test;
You can however use a constructor:
public function __construct()
{
$this->bdate = date("c");
}
Problem
I think this is a really, really simple question but I can't seem to find the answer. I'm trying to set default values for properties in a PHP class and one of them is a date so I'm trying to use the date() function to set it: ``` <?php class Person { public $fname = ""; public $lname = ""; public $bdate = date("c"); public $experience = 0; } ?> ``` I'm getting an error in Zend Studio that on the public $bdate line that says ``` PHP Parse error: syntax error, unexpected '(', expecting ',' or ';' ``` I've looked through a bunch of documentation on the various date classes in PHP and the Class/Properties documentation but I can't find any examples of setting a date value to a property. I've tried mktime() and some of the other date classes but I always get the same error. How do you set a date value to a property in PHP?