开发者

Setting date to a variable in a PHP class

开发者 https://www.devze.com 2022-12-17 13:07 出处:网络
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

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:

&开发者_Go百科lt;?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?


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");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消