【发布时间】:2011-10-22 08:39:57
【问题描述】:
我正在开发一个类,我有这样的结构:
class userInfo {
public $interval = 60;
public $av_langs = null;
public $ui_ip = $_SERVER['REMOTE_ADDR'];
public $ui_user_agent = $_SERVER['HTTP_USER_AGENT'];
public $ui_lang = null;
public $ui_country = null;
// non-relevant code removed
}
但在执行脚本时出现此错误:
解析错误:语法错误,意外的 T_VARIABLE 在 D:\web\www\poll\get_user_info\get_user_info.php 在第 12 行
当我将 2 个 $_SERVER 变量更改为简单字符串时,错误消失了。
那么 $_SERVER 在声明类属性时有什么问题呢?
谢谢
【问题讨论】:
-
以上不是变量赋值,而是类属性声明。因此,它们不能保存表达式。那里不允许。您必须使用常量或在构造函数中使用。 php.net/manual/en/language.oop5.properties.php
标签: php class properties