Answers.Body: <p>What I do is I listen for the <code>Plugins::EVENT_AFTER_LOAD_PLUGINS</code> event, and then do my code that relies on other plugins being loaded:</p>
  
  <pre><code>    // Handler: Plugins::EVENT_AFTER_LOAD_PLUGINS
      Event::on(
          Plugins::class,
          Plugins::EVENT_AFTER_LOAD_PLUGINS,
          function () {
              // Do stuff only after all other plugins have loaded
          }
      );
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-06T23:36:42.513Z
Answers.Text: Yes, we have this event, and it is working like charm, but in this case is not good. I have to make a decision in realtime (routing decision for cache). Is there any way to manually instantiate the plugin and store it into the proper place?
  I am trying to use something like this... but guess this is not the best method:(          $mobile_detect = (new \superbig\mobiledetect\services\MobileDetectService())->getMobileDetect();
Answers.UserDisplayName: Visky Máté
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-06T23:35:12.513Z
Answers.LastActivityDate: 2018-11-06T23:35:12.513Z
Answers.UserDisplayName: andrew.welch
Body: <p>I have a plugin, which is using another plugin. The issue is that, I am calling some other plugin information in the init function, and I do receive sometimes this error:</p>
  
  <pre><code>2018-11-07 11:44:57 [-][-][k91hcavi6st542afnhsvhdp61u][error][yii\base\ErrorException:8] yii\base\ErrorException: Trying to get property 'mobileDetectService' of non-object in /home/forge/plugins/releases/20181106223004/src/services/CacheService.php:193
  Stack trace:
  #0 /home/forge/new.trendsideas.com/releases/20181106223038/vendor/craftcms/cms/src/web/ErrorHandler.php(76): yii\base\ErrorHandler-&gt;handleError(8, 'Trying to get p...', '/home/forge/plu...', 193)
  #1 /home/forge/plugins/releases/20181106223004/src/services/CacheService.php(193): craft\web\ErrorHandler-&gt;handleError(8, 'Trying to get p...', '/home/forge/plu...', 193, Array)
  #2 /home/forge/plugins/releases/20181106223004/src/services/CacheService.php(126): trendspublishing\craftplugin\services\CacheService-&gt;_generateUri('/hubs/bathroom')
  #3 /home/forge/plugins/releases/20181106223004/src/Plugin.php(90): trendspublishing\craftplugin\services\CacheService-&gt;uriToFilePath('/hubs/bathroom')
  #4 /home/forge/new.trendsideas.com/releases/20181106223038/vendor/yiisoft/yii2/base/BaseObject.php(109): trendspublishing\craftplugin\Plugin-&gt;init()
  #5 /home/forge/new.trendsideas.com/releases/20181106223038/vendor/yiisoft/yii2/base/Module.php(158): yii\base\BaseObject-&gt;__construct(Array)
  #6 /home/forge/new.trendsideas.com/releases/20181106223038/vendor/craftcms/cms/src/base/Plugin.php(113): yii\base\Module-&gt;__construct('craftplugin', Object(craft\web\Application), Array)
  #7 [internal function]: craft\base\Plugin-&gt;__construct('craftplugin', Object(craft\web\Application), Array)
  </code></pre>
  
  <p>This is the error in question:</p>
  
  <pre><code>$mobile_detect = MobileDetect::$plugin-&gt;mobileDetectService;
  </code></pre>
  
  <p>I am not able to replicate it, sometimes a simple database reload is helping sometimes not. So that's why I am only guessing, because I do invoke this call from the plugin init function, that's why the other plugin is not ready yet...
  Any thought, best practice on this one?</p>
  
Tags: <plugin-development>
Title: Plugin init order issue
ThreadText: # Plugin init order issue
  
  (Asked by: username_0 on 2018-11-06)
  
  I have a plugin, which is using another plugin. The issue is that, I am calling some other plugin information in the init function, and I do receive sometimes this error:
  
  ```
  2018-11-07 11:44:57 [-][-][k91hcavi6st542afnhsvhdp61u][error][yii\base\ErrorException:8] yii\base\ErrorException: Trying to get property 'mobileDetectService' of non-object in /home/forge/plugins/releases/20181106223004/src/services/CacheService.php:193
  Stack trace:
  #0 /home/forge/new.trendsideas.com/releases/20181106223038/vendor/craftcms/cms/src/web/ErrorHandler.php(76): yii\base\ErrorHandler->handleError(8, 'Trying to get p...', '/home/forge/plu...', 193)
  #1 /home/forge/plugins/releases/20181106223004/src/services/CacheService.php(193): craft\web\ErrorHandler->handleError(8, 'Trying to get p...', '/home/forge/plu...', 193, Array)
  #2 /home/forge/plugins/releases/20181106223004/src/services/CacheService.php(126): trendspublishing\craftplugin\services\CacheService->_generateUri('/hubs/bathroom')
  #3 /home/forge/plugins/releases/20181106223004/src/Plugin.php(90): trendspublishing\craftplugin\services\CacheService->uriToFilePath('/hubs/bathroom')
  #4 /home/forge/new.trendsideas.com/releases/20181106223038/vendor/yiisoft/yii2/base/BaseObject.php(109): trendspublishing\craftplugin\Plugin->init()
  #5 /home/forge/new.trendsideas.com/releases/20181106223038/vendor/yiisoft/yii2/base/Module.php(158): yii\base\BaseObject->__construct(Array)
  #6 /home/forge/new.trendsideas.com/releases/20181106223038/vendor/craftcms/cms/src/base/Plugin.php(113): yii\base\Module->__construct('craftplugin', Object(craft\web\Application), Array)
  #7 [internal function]: craft\base\Plugin->__construct('craftplugin', Object(craft\web\Application), Array)
  
  ```
  
  This is the error in question:
  
  ```
  $mobile_detect = MobileDetect::$plugin->mobileDetectService;
  
  ```
  
  I am not able to replicate it, sometimes a simple database reload is helping sometimes not. So that's why I am only guessing, because I do invoke this call from the plugin init function, that's why the other plugin is not ready yet...
  Any thought, best practice on this one?
  
  **Tags:** plugin-development
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-11-06. Score: 4
  
  What I do is I listen for the `Plugins::EVENT_AFTER_LOAD_PLUGINS` event, and then do my code that relies on other plugins being loaded:
  
  ```
      // Handler: Plugins::EVENT_AFTER_LOAD_PLUGINS
      Event::on(
          Plugins::class,
          Plugins::EVENT_AFTER_LOAD_PLUGINS,
          function () {
              // Do stuff only after all other plugins have loaded
          }
      );
  
  ```
  
  > **Comments:**
  > **username_0**: Yes, we have this event, and it is working like charm, but in this case is not good. I have to make a decision in realtime (routing decision for cache). Is there any way to manually instantiate the plugin and store it into the proper place? I am trying to use something like this... but guess this is not the best method:(          $mobile_detect = (new \superbig\mobiledetect\services\MobileDetectService())->getMobileDetect();  
  
  ---
  
  