普通网友 2015-06-27 01:47
浏览 41
已采纳

PHP $这里面使用类来引用类

I have a function loadModule(); in class core, but to load modules I need to define variables in the construct, and many of them require core. Would I simply use loadModule("someModule", $settings, $dbc, $core, $etc...); or loadModule("someModule", $settings, $dbc, $this, $etc...); since this function is in the core class that is defined by $core? I'm confused right now, and help would be appreciated. Thanks

EDIT:

Intended use would look something like $this->core->loadModule("initialLoad, $settings, $version, $dbc, $parser, $layout);

The module construct would look like

public function __construct($settings, $version, $dbc, $layout, $core, $parser){
    $this->settings = $settings;
    $this->version = $version;
    $this->dbc = $dbc;
    $this->layout = $layout;
    $this->core = $core;
    $this->parser = $parser;

}
  • 写回答

1条回答 默认 最新

  • douwa1304 2015-06-27 02:42
    关注

    I would look into dependency injection. Perhaps use something like pimple.

    Have your container initialize all your modules. If you need to get into more advance use you could probably proxy the services so that they're lazy instantiate at first time use.

    Then you could probably inject core into the classes and use the loaded modules however you like.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答