开发者

Is there a reason I cannot declare public in class?

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜