Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Fatal error: Constant expression contains invalid operations

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

like image 485
pixie123 Avatar asked Jan 02 '17 00:01

pixie123


1 Answers

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

like image 93
funsholaniyi Avatar answered Oct 29 '22 12:10

funsholaniyi