OO PHP Accessing public variable from another class

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

    0 讨论(0)
  • 2021-02-02 15:33

    Since the property is public, you can access it from outside the class as $objInstance->property. It doesn't matter if you're calling it from a function, procedural script, in another object. As long as you have the instance, you can call it's public property. Ex:

    function foo($c) {
        echo $c->lang;
    }
    foo($check);
    

    Also, some advice on working with objects and such: It's considered better code if you don't create instances of objects in the other objects, but rather pass them in someway (either a setter method or through the constructor). This keeps the classes loosely coupled and results in code that is more reusable and easier to test. So:

    class Game
    {
    
    ...
    public function __construct($check, $defaultLang, $get) {
    
        $this->check = $check;
    
        $this->lang = $defaultLang;
        if (isset($get['lang']) && !$this->check->isEmpty($get['lang']))
            $this->lang = $get['lang'];
    }
    ...
    
    $game = new Game(new Check(), DEFAULT_LANG, $_GET);
    echo $game->check;
    

    The first half of this article is an accessible explanation of what is known as Dependency Injection.

    0 讨论(0)
  • 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!

    <?php
    define('TEST', 'xxx');
    
    class game {
        public function __construct() {
            echo TEST;
        }
    }
    
    //prints 'xxx'
    new game;
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题