OO PHP Accessing public variable from another class

前端 未结 4 1121
借酒劲吻你
借酒劲吻你 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

    if you mark the public $lang; as static:

    public static $lang;
    

    you can access it via game::$lang;

    if not static, you need to make an instance of game and directly access it:

    $game = new game;
    $game->lang;
    

    static call inside of (current) class:

    self::$lang;
    

    late static bound call (to inherited static variable):

    static::$lang;
    

    call from child class to parent:

    parent::$lang;
    

    normal call inside of an instance (instance is when you use new Obj();):

    $this->lang;
    

    BTW: variables defined by define('DEFAULT_LANG', 'en_EN'); are GLOBAL scope, mean, can access everywhere!

提交回复
热议问题