Error: Constant Expression contains invalid Operations

Asked

Viewed 446 times

1

I have a class I’m trying to pick up Session from this way:

session_start();
class metodoClass{ 
    ....
    public $usuario = $_SESSION["Usuario"];
    ....
}

But when I do that, it makes the mistake: Constant Expression contains invalid Operations.

How can I fix this?

1 answer

2


I didn’t dig deep, but I believe that at the moment of class definition the super global $_SESSION is not yet available.

You can set this value at the time of class instantiation:

<?php

session_start();

class metodoClass{ 

    public $usuario;

    public function __construct() {
        $this->usuario = $_SESSION["Usuario"];
    }

}

Behold working.

  • Thank you gmsantos.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.