I'm developing a class and I have this structure:
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
}
But when executing the script I get this error:
Parse error: syntax error, unexpected T_VARIABLE in D:\web\www\poll\get_user_info\get_user_info.php on line 12
When I changed the 2 $_SERVER vars to simple strings the error disappeared.
So what's the problem with $_SERVER in declaring class properties?
Thanks
Use this code as a guide:
public function __construct() {
$this->ui_ip = $_SERVER['REMOTE_ADDR'];
$this->ui_user_agent = $_SERVER['HTTP_USER_AGENT'];
}
Property can be declared only with value, not expression.
You can create __construct() method, where you can initialize properties in any way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With