PHP Fatal error: Constant expression contains invalid operations
PHP Fatal error: Constant expression contains invalid operations
这是致命错误:
Fatal error: Constant expression contains invalid operations
我在这段代码中遇到致命错误:
<?php
class InfoClass {
private $user_agent = $_SERVER['HTTP_USER_AGENT']; // error is on this line
public static function getOS() {
global $user_agent;
$os_platform = "Unknown OS Platform";
...
}
我正在使用php 7. 为什么会出现这个错误?谢谢
改为这样做
<?php
class InfoClass {
private $user_agent;
public function __construct(){
$this->user_agent = $_SERVER['HTTP_USER_AGENT']; // error is on this line
}
public static function getOS() {
global $user_agent;
$os_platform = "Unknown OS Platform";
...
}
希望对您有所帮助
这是致命错误:
Fatal error: Constant expression contains invalid operations
我在这段代码中遇到致命错误:
<?php
class InfoClass {
private $user_agent = $_SERVER['HTTP_USER_AGENT']; // error is on this line
public static function getOS() {
global $user_agent;
$os_platform = "Unknown OS Platform";
...
}
我正在使用php 7. 为什么会出现这个错误?谢谢
改为这样做
<?php
class InfoClass {
private $user_agent;
public function __construct(){
$this->user_agent = $_SERVER['HTTP_USER_AGENT']; // error is on this line
}
public static function getOS() {
global $user_agent;
$os_platform = "Unknown OS Platform";
...
}
希望对您有所帮助