简体   繁体   English

快速OOP问题 - PHP

[英]Quick OOP Question - PHP

In OOP, sometimes you see something similar to this: 在OOP中,有时您会看到与此类似的内容:

$memberID = $system->members->memberID();

I was wondering and totally confused on the part where it is ->members->... How does that work? 我对它所在的部分感到疑惑和困惑->members->...这是如何工作的?

For example, lets say I have a class that I call up called $systems , then how can I put ->members-> after it to run the members class? 例如,假设我有一个我称之为$systems ,那么如何在它之后运行->members->来运行成员类?

I only know how to do something along the lines of this: 我只知道如何做到这一点:

$system = new system();
$memberID = $system->memberID();

But I would like to know how to do this: 但我想知道如何做到这一点:

$system = new system();
$memberID = $system->members->memberID();

Thanks! 谢谢!

-- UPDATE -- Here's a little update, thanks to everyone who helped me out this far! - 更新 - 这是一个小小的更新,感谢所有帮助我的人! You guys really pointed me in the right direction, I actually have a great answer to my own question! 你们真的指出了我正确的方向,我真的对我自己的问题有一个很好的答案! :) And thanks to the moderator who edited this question, I'm sorry I wasn't familiar with the bbcode syntax. :)感谢主持人编辑过这个问题,对不起,我对bbcode语法并不熟悉。

I wanted something to automatically make the new classes, for example calling ->members-> would be automatically included using __get() rather then having to do manually put in something like "new members()". 我想要一些东西自动创建新类,例如调用 - > members->将自动包含__get()而不是必须手动输入类似“new members()”的东西。 A little difficult for me to explain, but I hope you got the basics of it. 我有点难以解释,但我希望你掌握它的基础知识。

Anyhow, here is the code that I use: 无论如何,这是我使用的代码:

<? class system {

public function __get($name){
    $file = 'lib/'.$name;
    if(file_exists($file)){
        require_once($file);
        $classname = $name;
        $this->$name = new $classname($this);
        return $this->$name;
    }else{
        die('Class '.$name.' could not be loaded (tried to load class-file '.$file.')');
    }
}

} ?> }?>

Now, if I were to do something the lines of this: 现在,如果我要做一些这样的事情:

$system = new system();
$system->members->functionHere();

It would automatically create a new instance of the members class and require the file from the lib folder. 它会自动创建成员类的新实例,并需要lib文件夹中的文件。

If this is against the rules then I apologize. 如果这违反规则,那么我道歉。 I just wanted to post this for anyone who came across this question while searching Google, because I know I always land up here when googling things! 我只是想在搜索Google时遇到这个问题的任何人发布这个帖子,因为我知道在google搜索时我总是会在这里登陆!

the $system variable holds an object which has a property named $members which itself holds an object which has a property $memberID $system变量包含一个对象,该对象具有名为$members的属性,该属性本身包含一个具有属性$memberID的对象

$system = new system();
$system->members = new Members(); // or whatever it must be
$system->members->memberId();

When you see something like that, you know that someone has most probably done something wrong! 当你看到类似的东西时,你知道有人最有可能做错了什么!

In order for this code to work, you need to grant public access to a member variable of an object (the former storing an object). 为了使此代码有效,您需要授予对象的成员变量的公共访问权限(前者存储对象)。

To grant public access to such a member variable is in most cases bad practice. 在大多数情况下,允许公共访问此类成员变量是不好的做法。 The variable should only be accessible through a getter (at least, it will still violate the LoD). 该变量只能通过getter访问(至少,它仍然会违反LoD)。

This code breaks the principle of encapsulation and the LoD (Law of Demeter). 该代码打破了封装原理和LoD(Demeter法则)。

[EDIT] [编辑]

Why it is almost certainly a mistake: 为什么几乎肯定是一个错误:

A) Granting direct public access to member variables is in most cases a mistake, because it makes the public interface of your class rigid (hard to change). A)在大多数情况下,授予对成员变量的直接公共访问权是一个错误,因为它会使您的类的公共接口变得僵硬 (难以更改)。 If you have a getter, you can change the implementation of the member anytime, the getter will still be the same and you don't need to change the call from anywhere. 如果你有一个getter,你可以随时更改成员的实现,getter仍然是相同的,你不需要从任何地方更改调用。 You can NEVER write a proxy for direct access to a variable! 您永远不能编写代理来直接访问变量! Writing a proxy for a getter on the other hand is easy! 另一方面,为getter编写代理很容易!

B) Granting direct public access to member variables is in most cases a mistake, because you let everyone talk to a class inside a class directly! B)授予对成员变量的直接公共访问权限在大多数情况下都是错误的,因为您可以让每个人直接与班级内的班级交谈 This will most probably lead to higher maintainance costs when the public interface of any of these two classes changes. 当这两个类中的任何一个的公共接口发生变化时,这很可能会导致更高的维护成本

[/EDIT] [/编辑]

members is object property of system and is also an object that contains method memberID() . memberssystem对象属性,也是包含方法memberID()的对象。

To assign property to your object, simply do something like this: 要将属性分配给对象,只需执行以下操作:

class System {
  function __construct() {
    $this->members = new Members();
  }

  // etc
}

or 要么

$systemObj = new System();
$systemObj->members = new Members();

It really really depends on the context you wish to use :) 这真的取决于你想要使用的上下文:)

As @markus mentioned, properties must be declared public if you're accessing them from outside. 正如@markus所提到的,如果您从外部访问属性,则必须将属性声明为公共属性。 Also, using setters/getters is often much better ... 此外,使用setter / getters通常要好得多......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM