开发者

Is there a reason I cannot declare public in class?

开发者 https://www.devze.com 2023-02-13 06:38 出处:网络
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=$_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

精彩评论

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