具有静态串联字符串变量的奇怪解析错误 [英] Strange parse error with static concatenated string variable

查看:85
本文介绍了具有静态串联字符串变量的奇怪解析错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到此错误:


解析错误:语法错误,意外的'。',期望为','或';在第5行的/var/(....../config.php)中

Parse error: syntax error, unexpected '.', expecting ',' or ';' in /var/(...)/config.php on line 5

使用以下(简化的)代码:

With this (simplified) code:

<?php

class Config
{
   public static $somevar = "Date: " . date('Y');
}

?>

我认为这是有效的php,但我想不是……我在这里做错什么了吗?谢谢!

I thought this was valid php, but I guess not... what am I doing wrong here? Thanks!

推荐答案

根据 PHP文档


就像其他任何PHP静态变量一样,静态属性可能仅使用文字或常量进行初始化;不允许使用表达式。因此,尽管您可以将静态属性初始化为整数或数组(例如),但不能将其初始化为另一个变量,函数返回值或对象。

Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object.

尝试写作

Config::$somevar = "Date: " . date('Y');

在类定义之后。

这篇关于具有静态串联字符串变量的奇怪解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆