Jump to content

Modules init order


Recommended Posts

It depends on whether you are talking about 'autoload' modules or not. If the dependent module is 'autoload' and the dependency module isn't, then your dependent module can just load the dependency in it's init() or ready(), like this:

public function init() {
 $dependency = $this->modules->get('YourDependencyModule'); 

If the dependency module is 'autoload' and the dependent module isn't, then then dependent module can safely assume the dependency is already loaded.

If both of them are 'autoload' then you don't necessarily know which will get loaded first. But by the time either module's ready() function is called, you know that both have been loaded:

public function ready() {
 // safe to assume all autoload modules are loaded and have had their init() called

If the dependent actually extends the dependency (like one class extending another), or has code that needs stuff defined from the dependency before PHP will even parse it, then you need the dependency to have been included as a file first (like you'd expect anywhere else in PHP). As long as you reference the dependency class name in some form, it should trigger PW's class loader to find and include the file. Something like a "Dependent extends Dependency" class declaration should do that, as should a function call like: Dependency::getModuleInfo(). If you find that still doesn't do it, you can also try including it manually. But use the "once" version, i.e. include_once($file); or require_once($file). However, I don't think you'll have to go as far as doing this manual inclusion.

Install Dependency

Because load order dependencies don't usually require any intervention on your part, we usually think of module dependencies as being install related. ProcessWire has some things to help you in this area. You can specify this in your getModuleInfo() method:

static public function getModuleInfo() {
 return array(
  'title' => 'Module title',
  'summary' => 'What the module does',
  'version' => 100, 
  'autoload' => true,
  'singular' => true, 
  'requires' => array('YourDependencyModule') // SEE THIS LINE

Here are all the details about module dependencies.

  • 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

  • Create New...