Jump to content

Instantiate one module inside of another


Recommended Posts

I apologize if this question doesn't make sense... I'm still getting used to OOP.

Is it possible to instantiate a module inside of another module? In other words... I would like to be able to create my own API calls like $modules->myModule1->myModule2->myFunction() similar to the ProcessWire core.

I figured out how to do this with a generic class by including the class in the module file and instantiating it in the constructor function, but I would like to be able to do it with a module as well (to make installation, etc. more user friendly).

Link to comment
Share on other sites

Why would you need method chaing for two modules? Of course you could do it, but why not:

// in Template
$myModule = $modules->get('myModule');
$myModule2 = $modules->get('myModule2');
// Call a public method of your module class
$myModule->renderSomethingNice();

// You can load your module anywhere, for example in another module
$myModule = $this->modules->get('myModule');
$myModule->renderSomethingNice();

The trick with method chainings is that you return back the instance ($this) of your current object, for example:

class Test {

public function method1() {
  // do something
  return $this;
}

public function method2() {
  // do something else
  return $this;
}

}

// Now when using your class...
$test = new Test();
$test->method1()->method2();
Link to comment
Share on other sites

The reason is that myModule2 could be any one of several modules that implement a basic class template, and I don't want to have to reference the specific module by name in order to make use of it in my code. myModule2 is also closely tied to the functionality of myModule1 and would never be used apart from it. In other words, it's a submodule of myModule1.

So I want to be able to reference it generically from within myModule1 and/or within my template by making it a subobject which is instantiated as a property in myModule1 (the specific module used is based on the status of a select box in the admin settings of myModule1).

Perhaps this isn't the best way of going about it, but I like the aesthetics of it.

Link to comment
Share on other sites

$this->submodule = wire("modules")->get("submodule");

..Call in init().

Use in template.

$m $modules->get("mainmodule");

$m->submodule->subfunction();

--- or creating a template var

$this->fuel->set("submodule", wire("modules")->get("submodule"));

..call in init().

Use in template:

$submodule->subfunction();

  • Like 1
Link to comment
Share on other sites

.. or combine above answers to get a method that instantiates and returns your submodule when/if needed:

public function submodule() {
    return wire('modules')->get("myModule2");
}

$modules->get("myModule")->submodule()->doSomething();

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...