OO PHP 从另一个类访问公共变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17576843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:07:25  来源:igfitidea点击:

OO PHP Accessing public variable from another class

phpoop

提问by Daniel W.

I have a class like the following:

我有一个如下所示的类:

class game {

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

    public function __construct() {

        $this->check = new check();

        $this->lang = DEFAULT_LANG;
        if (isset($_GET['lang']) && !$this->check->isEmpty($_GET['lang']))
            $this->lang = $_GET['lang'];
    }

}

As you can see I have a public variable $langthat is also defined via the contructor.

如您所见,我有一个$lang也是通过构造函数定义的公共变量。

The proble is that I want to access the result of this variable from other classes that are not directly related to this class, since I don't want to redeclare it for each different class.

问题是我想从与这个类没有直接关系的其他类访问这个变量的结果,因为我不想为每个不同的类重新声明它。

So for example how can I call the result of that variable from another class, lets call it class Check?

例如,我如何从另一个类调用该变量的结果,让我们调用它class Check

回答by Daniel W.

if you mark the public $lang;as static:

如果您将其标记public $lang;为静态:

public static $lang;

you can access it via game::$lang;

你可以通过 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 boundcall (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();):

实例内部的正常调用(实例是您使用时new Obj();):

$this->lang;

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

顺便说一句:定义的变量define('DEFAULT_LANG', 'en_EN');是全局范围的,意思是,到处都可以访问!

<?php
define('TEST', 'xxx');

class game {
    public function __construct() {
        echo TEST;
    }
}

//prints 'xxx'
new game;

回答by roy

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.

静态类的思想是它只存在于一个实例中,所以你的程序中只存在一个 $lang 。但是没有必要加载整个类来访问它。

回答by Shoe

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 $objof class gamethen you can simply do:

变量没有结果。如果您想在$obj类的特定对象上检索该变量的状态,game那么您可以简单地执行以下操作:

$obj->lang

On a side note if $langis publicly only read only you should protect it by defining it privateor protectedand create a getter methodinstead.

附带说明一下,如果$lang是公开只读的,您应该通过定义它privateprotected创建一个getter 方法来保护它。

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.

但是两个对象的变量会不同。

回答by Timothy

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:

由于该属性是公共的,您可以从类外部以$objInstance->property. 如果您从另一个对象中的函数、过程脚本调用它并不重要。只要你有实例,你就可以称它为公共财产。前任:

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:

此外,关于使用对象等的一些建议:如果您不在其他对象中创建对象的实例,而是以某种方式(通过 setter 方法或通过构造函数)传递它们,则被认为是更好的代码。这使类保持松散耦合,并产生更可重用且更易于测试的代码。所以:

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 articleis an accessible explanation of what is known as Dependency Injection.

上半年这篇文章是所谓依赖注入可访问的解释。