OO PHP Accessing public variable from another class

前端 未结 4 1145
借酒劲吻你
借酒劲吻你 2021-02-02 14:46

I have a class like the following:

class game {

    public $db;
    public $check;
    public $lang;

    public function __construct() {

        $this->che         


        
4条回答
  •  名媛妹妹
    2021-02-02 15:49

    you can make it static variable, so you will be able to call it anytime anywhere, the diff is that instead of

    $this->lang;
    

    when editing it(Works inside class game only) you do :

    self::$lang;
    

    and when you call/edit it (Works everywhere) from anther class you do :

    game::$lang
    

    the idea of static class is that its exist only in one instance, so only one $lang exist in your program. but there is no need to load the whole class to get acsess to it.

提交回复
热议问题