Is there a reason why I cannot declare a publication in the class?

class struct {
public $variable=$_SESSION['Example'];
}

How can I call a session and put it in a variable in php classes?

+3
source share
4 answers

Read http://php.net/manual/en/language.oop5.php

class struct {
 public $variable;
 public function __construct(){
   session_start();
   $this->variable = $_SESSION['Example'];
 }
}
+6
source
class struct {
  public $variable;

  public function __construct(){
    session_start();
    $this->variable = $_SESSION['Example'];
  }
}
+3
source

Properties can only have default literals, not arbitrary expressions. The easiest way to do this:

class Struct {
    public $variable;

    public function __construct() {
        $this->variable = $_SESSION['Example'];
    }
}
+3
source

You cannot set any properties in a definition unless they are a constant, for example. TRUE, array()etc.

In __construct()you can install it.

+2
source

Source: https://habr.com/ru/post/1794344/


All Articles