PHP Fatal error: Constant expression contains invalid operations

北战南征 提交于 2020-06-12 05:35:06

问题


here is the fatal error:

Fatal error: Constant expression contains invalid operations

I get a fatal error in this code:

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

        ...
}

i am using php 7. why is this error showing? thanks


回答1:


Do This Instead

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

    ...
}

Hope it Helps



来源:https://stackoverflow.com/questions/41420147/php-fatal-error-constant-expression-contains-invalid-operations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!