类的属性不能使用时间函数啊?

WBOY
Release: 2016-06-23 14:16:52
Original
865 people have browsed it

代码如下,直接报错,我想做的事情是, 访问一个类的属性,返回当前时间,因为我使用的是thinkphp,里面有一个地方一定是这样的:访问类属性返回当前时间,但是我这样写不行,出错,请问怎么做?

<?phpclass Mytime{public $time=date('Y-m-d H:i:s');}$mytime=new Mytime();$mes=$mytime->time;echo $mes;
Copy after login



出错信息如下:
Parse error: syntax error, unexpected '(', expecting ',' or ';' in C:\php\apache\htdocs\test5.php on line 5
Copy after login


回复讨论(解决方案)

class Mytime{
  public $time;
  function __construct() {
    $this->time = date('Y-m-d H:i:s');
  }
}

定义类时,类属性的值不能是可变的量

class Mytime{
  public $time;
  function __construct() {
    $this->time = date('Y-m-d H:i:s');
  }
}

定义类时,类属性的值不能是可变的量

版主真乃神人也

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template