dougai2427 2013-11-03 22:10
浏览 41

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

Let' suppose I have my code organized in classes and each class has its own file:

  • main.php, having class Main
  • config.php having class Config
  • security.php having class Security
  • database.php having class Database

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. The problem is that now Security might need something (a variable or function) from Config and Database something from Security.

// 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? Of course, I could send those variables as arguments to the constructors but what happens when we need like 5 or 10 of them? I could send the entire object Config to Security and Security to 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++)? Maybe I can send the hole Main object's reference as an argument in the constructor, and this way make everything available for everything.

// 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?

  • 写回答

1条回答 默认 最新

  • duanqiao2225 2013-12-17 12:09
    关注

    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). 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). 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. 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.

    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)

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?