php 初始化多变量吗,PHP类中变量的初始化只能是定值

首先看代码:

以下是引用片段:

杯具了,这句话有语法错误:

以下是代码片段:

private $_expire_time =  60 * 60 * 24 * 7;

那么,这是为什么呢?

以下是引用片段:

Class member variables are called "properties". You may also see them referred to using other terms such as "attributes" or "fields", but for the purposes of this reference we will use "properties". They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

如红色字体,翻译如下:

以下是引用片段:

这个声明可能会包含初始化,当时这个初始化必须是一个定值,也就是说,这个定值必须在编译时就能确定,而不是依赖于php在运行时再确定值。

呃,翻译的有点拗口。简而言之,就是说类里面变量的初始化不能是一个表达式,否则编译期间就编译不过去,产生不了Opcodes。

手册中给出错误和正确的初始化的例子:

以下是代码片段:

// invalid property declarations:

public $var1 = ’hello ’ . ’world’;

public $var2 = <

EOD;

public $var3 = 1+2;

public $var4 = self::myStaticMethod();

public $var5 = $myVar;

// valid property declarations:   public $var6 = myConstant;

public $var7 = array(true, false);

// This is allowed only in PHP 5.3.0 and later.   public $var8 = <<

hello world

EOD;

}

?>

关于Opcodes和编译的过程,可以参考以下资料:

说句题外话:

不过随便进入文档任意语言页面,切换语言中选 "Other",还是可以进入中文文档的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值