PHP致命错误:常量表达式包含无效操作

时间:2017-01-02 00:12:03

标签: php function class error-handling fatal-error

这是致命的错误:

  

致命错误:常量表达式包含无效操作

我在这段代码中遇到致命错误:

<?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.为什么会出现此错误?感谢

1 个答案:

答案 0 :(得分:4)

执行此操作

<?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";

    ...
}

希望它有助于