[英]I cannot use IF-THEN-ELSE and $_SERVER variables in PHP class definition
class MY_CONFIG {
if ( $_SERVER["SERVER_ADDR"] == "127.0.0.1" ) {
var $default = array( 'foo' => 1, 'bar' => 2 );
} else {
var $default = array( 'foo' => 3, 'bar' => 4 );
}
}
I am new to PHP.我是 PHP 的新手。 What's wrong with my code above?
我上面的代码有什么问题?
The system keeps saying:系统一直在说:
Parse error: syntax error, unexpected T_IF, expecting T_FUNCTION in C:\wamp\www\test\class.php on line 4解析错误:语法错误,意外的 T_IF,在第 4 行的 C:\wamp\www\test\class.php 中需要 T_FUNCTION
Thanks.谢谢。
-- Hin ——欣
vars in a class definition have to be static. class 定义中的变量必须是 static。 if you need to apply logic to them, it should be in the constructor:
如果你需要对它们应用逻辑,它应该在构造函数中:
class MY_CONFIG {
var $default = array('foo' => 3, 'bar' => 4);
public function __construct() {
if ( $_SERVER["SERVER_ADDR"] == "127.0.0.1" ) {
$this->default = array( 'foo' => 1, 'bar' => 2 );
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.