简体   繁体   English

PHP-从嵌套类访问父类成员

[英]PHP - access parent class members from nested classes

Let' suppose I have my code organized in classes and each class has its own file: 让我们假设我的代码按类组织,并且每个类都有自己的文件:

  • main.php , having class Main main.php ,具有Main
  • config.php having class Config 具有类Config的 config.php
  • security.php having class Security security.php上课安全
  • database.php having class Database database.php中上课数据库

Now, Main 's constructor will initialize 3 objects, one for each of the other classes, and this way everything will look more or less like a class/subclass. 现在, Main的构造函数将初始化3个对象,每个其他类一个,这样,一切看起来或多或少都类似于类/子类。 The problem is that now Security might need something (a variable or function) from Config and Database something from Security. 问题在于,现在安全性可能需要Config中的某些内容(变量或函数),而数据库可能需要安全性中的某些内容。

// main.php
// here I include the other files
class Main {
    functions __constructor() {
        $this->Config = new Config();
        $this->Security = new Security();
        $this->Database = new Database();
    }
}

// config.php
class Config {
    public $MyPassword = '123456';
    public $LogFile    = 'logs.txt';
    // other variables and functions
}

// security.php
class Security {
    functions __constructor() {
        // NOW, HERE I NEED Config->Password
    }

    function log_error($error) {
        // HERE I NEED Config->LogFile
    }
}

// database.php
class Database {
    functions __constructor() {
        // Trying to connect to the database
        if (failed) {
            // HERE I NEED TO CALL Security->log_error('Connection failed');
        }
    }
}

So, how do I share those functions and variables between these nested classes inside Main ? 因此,如何在Main内部的这些嵌套类之间共享这些函数和变量? Of course, I could send those variables as arguments to the constructors but what happens when we need like 5 or 10 of them? 当然,我可以将这些变量作为参数发送给构造函数,但是当我们需要5或10个变量时会发生什么呢? I could send the entire object Config to Security and Security to Database , 我可以将整个对象Config发送到Security,并将Security发送到Database

// main.php
// here I include the other files
class Main {
    functions __constructor() {
        $this->Config = new Config();
        $this->Security = new Security($this->Config);
        $this->Database = new Database($this->Security);
    }
}

but is that reliable? 但这可靠吗? Can I send only the reference (like a pointer, in C++)? 我可以只发送引用(如C ++中的指针)吗? Maybe I can send the hole Main object's reference as an argument in the constructor, and this way make everything available for everything. 也许我可以在构造函数中将Hole Main对象的引用作为参数发送,并通过这种方式使所有内容对一切可用。

// main.php
// here I include the other files
class Main {
    functions __constructor() {
        $this->Config = new Config();
        $this->Security = new Security(&$this);
        $this->Database = new Database(&$this);
    }
}

I don't even know if this is possible. 我什至不知道这是否可能。 What do you think? 你怎么看? Are there any more conventional ways? 还有其他常规方法吗?

As it is stated in comments you are starting to think in terms alined with Dependency Injection. 正如评论中所指出的那样,您已经开始以依赖注入的术语来思考。 You are defensively coding (and rightly so) to workaround the issue of SoC (Separation of Concerns). 您正在采取防御性编码(正确的做法是)来解决SoC(关注分离)问题。 You might try like I did with something I call the Registry pattern (I'm ignorant on the subject so I named it after the windows registry). 您可能会尝试像我对注册表模式所做的那样(我对这个主题一无所知,因此以Windows注册表命名)。 The registry holds all the objects that may need to be passed around. 注册表包含所有可能需要传递的对象。 This gives some benefits on a practical level 这在实践上带来了一些好处

  • If I'm not sure something else is going to need a var, I just tack it into the registry and the one which depends will know where to look for it, as long as I pass him the Registry 如果我不确定是否还有其他需要的变种,我只要将其添加到注册表中即可,只要我将注册表传递给他,依赖它的对象就会知道在哪里寻找它
  • If my project is very small and I don't want to hassle around to much about the idea then this is an easy solution 如果我的项目很小,并且不想在这个想法上麻烦太多,那么这是一个简单的解决方案

There are quite a set of problems behind this pattern of thinking. 这种思维方式背后存在许多问题。 Say the project starts to get bigger, I know it happens to me sometimes. 假设项目开始变得越来越大,我知道有时会发生这种情况。 Now simple tasks like debugging become mountain climbing as I try to find why a dependancy is not where I'm looking for it and I have to track down where it is set and at what point, and if some other piece of code changed it and why. 现在,诸如调试之类的简单任务变得非常艰辛,因为我试图找出为什么依赖关系不在我要查找的地方,因此我必须跟踪它的设置位置以及在什么时候,以及是否有其他代码段更改了它,并且为什么。

All this means is that instead of following the principles of SoC, we just passed the concern onto a third object that now bears ALL the responsibility. 所有这些意味着,我们没有遵循SoC的原理,而是将关注点转移到了现在承担所有责任的第三个对象上。 This "registry" object is now responsible for too many things and any changes that happen to it will ripple through all your code. 现在,此“注册表”对象负责太多事情,发生的任何更改都将遍历您的所有代码。

From what I've read around SO and other tutorials, if you have an object that is juggling too many dependancies (let's say constructor with 10 parameters) then we are probably not doing things right. 根据我在SO和其他教程中所读到的内容,如果您有一个对象在处理过多的依赖关系(比如说带有10个参数的构造函数),那么我们可能做错了。

I hope someone else can chime in on this because I'm very interested on this subject but I have not been able to put it in practice (mainly due to ignorance) 我希望其他人对此有所了解,因为我对此主题非常感兴趣,但是我无法将其付诸实践(主要是由于无知)

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

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