class struct {
public $variable=$_SESSION['Example'];
}
How do I call a S开发者_JS百科ession and put it into a variable in php classes?
Read http://php.net/manual/en/language.oop5.php
class struct {
public $variable;
public function __construct(){
session_start();
$this->variable = $_SESSION['Example'];
}
}
class struct {
public $variable;
public function __construct(){
session_start();
$this->variable = $_SESSION['Example'];
}
}
Properties can only have literal defaults, not arbitrary expressions. The simplest way to do this is:
class Struct {
public $variable;
public function __construct() {
$this->variable = $_SESSION['Example'];
}
}
You can not set any properties in the definition unless they are a constant, e.g. TRUE
, array()
, etc.
In the __construct()
you may set it.
精彩评论