普通网友 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 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序