本文介绍了为什么我不能在类属性声明中进行数学运算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
http://php.net/manual/en/language.oop5.properties.php 显示了以下对我来说失败的示例.
http://php.net/manual/en/language.oop5.properties.php shows the following example which fails for me.
class SimpleClass{
private $var3 = 1+2;
}
解析错误:语法错误,意外的+",需要,"或;"....
知道为什么吗?
php 版本 5.4.9
php version 5.4.9
推荐答案
文档从未声明它是有效的属性声明.它特别说明了一个无效示例.
The documentation never states that it is a valid property declaration. It specifically states its an invalid example.
直接来自您的链接
<?php
class SimpleClass
{
// invalid property declarations:
public $var1 = 'hello ' . 'world';
public $var2 = <<<EOD
hello world
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 = <<<'EOD'
hello world
EOD;
}
?>
这就是为 PHP 定义语言语法的方式.
this is just how the language syntax is defined for PHP.
这篇关于为什么我不能在类属性声明中进行数学运算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!