将类链接在一起

时间:2012-12-02 15:44:53

标签: php oop

我创建PHP应用程序,我对架构有疑问。

我的应用中有一些系统。 例如,缓存系统输出系统应用程序系统等。 此外,我还有 Registry 将这个系统连接在一起。

以下是在应用程序中使用缓存的示例:

class Manager {

    public function __construct() {
        $registry = \Jx\System\JxRegistry::getInstance();
        $this->JxCache = $registry -> JxCache;
    }

    public function getAppInfo($JxId) {
        $someInfo = $this->JxCache->Applications->$JxId;
    }

}

但是这样我需要在任何地方调用 Registry ,我需要创建局部变量来存储对象。

我有另一个想法,如下:

class Kernel {
    public function __construct() {
        $this->JxCache = new JxCache;
    }
}
/* .. */
new Kernel();
/* .. */
class Manager extends Kernel {

    public function __construct() {

    }

    public function getAppInfo($JxId) {
        $someInfo = $this->JxCache->Applications->$JxId; //JxCache declared in class 'Kernel'
    }

}

哪种变体更正确? 你能建议你的变种吗? 谢谢

1 个答案:

答案 0 :(得分:1)

Yii框架有不同的方法,你可以喜欢它:

Yii behaviors

相关问题