OO PHP Accessing public variable from another class

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

    How can I call the result of that variable from another class, lets call it class Check?

    A variable doesn't have a result. If you mean to retrieve the state of that variable on a specific object $obj of class game then you can simply do:

    $obj->lang
    

    On a side note if $lang is publicly only read only you should protect it by defining it private or protected and create a getter method instead.

    If you mean that you want to use the same variable name in another class I'd suggest you to consider inheritance:

    class Check extends game { /* now Check has $lang */ }
    

    but the variable of the two objects will be different.

提交回复
热议问题