Search code examples
phpthisthis-pointer

PHP $this use inside class to reference class


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;

}

Solution

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