Answers.Body: <p>Where do you instantiate your <code>orders</code> component?</p>
  
  <p>As long as you're calling the <code>setComponents</code> early in your <code>init</code> method as follow, you should be able to access it anywhere in your plugin class:</p>
  
  <pre class="lang-php prettyprint-override"><code>...
  parent::init();
  self::$plugin = $this;
  $this-&gt;setComponents([
          'orders' =&gt; OrdersService::class,
  ]);
  ...
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-25T18:59:07.810Z
Answers.Text: Not sure how the services were getting registered before so that they worked in every other module file except the primary module class, but in any case this worked. Thanks for your help!
Answers.UserDisplayName: Joel
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-22T11:16:55.473Z
Answers.LastActivityDate: 2019-06-22T11:16:55.473Z
Answers.UserDisplayName: Alban Jubert
Answers.Body: <p>If this is a module, the <code>self::$plugin = $this;</code> won't work.</p>
  <p>You gotta register your component thru app.php</p>
  <pre><code>use modules\sitemodule\Orders;
  use modules\sitemodule\services\OrdersService;
  
  return [
      'modules' =&gt; [
          'sitemodule' =&gt; [
              'class' =&gt; Orders::class,
              'components' =&gt; [
                  'ordersService' =&gt; OrdersService::class
              ]
          ]
      ],
      'bootstrap' =&gt; ['sitemodule']
  ];
  </code></pre>
  <p>Now you can call it in your controller like...</p>
  <pre><code>Orders::getInstance()-&gt;ordersService-&gt;doSomething();
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-02-11T17:02:56.683Z
Answers.LastActivityDate: 2022-02-12T12:28:59.747Z
Answers.LastEditDate: 2022-02-12T12:28:59.747Z
Answers.UserDisplayName: Leo Leoncio
Body: <p>I created a module for Craft 3 using pluginfactory.io, and am unable to call services from the primary module class using <code>$this-&gt;orders-&gt;method()</code> as shown in <a href="https://docs.craftcms.com/v3/extend/services.html#calling-service-methods" rel="nofollow noreferrer">the documentation</a>.</p>
  
  <p>The services appear to be registered correctly, as I'm able to call the service methods elsewhere in the module using the <code>SiteModule::$instance-&gt;orders-&gt;method()</code> format.</p>
  
  <p>The line <code>use modules\sitemodule\services\Orders as OrdersService</code> is at the top of the module class, but removing makes no difference in being able to call those service methods from the primary module class.</p>
  
  <p>What is the correct way to call service methods from a primary module class?</p>
  
Tags: <craft3><modules>
Title: Trouble calling service methods from primary module class
ThreadText: # Trouble calling service methods from primary module class
  
  (Asked by: username_0 on 2019-06-22)
  
  I created a module for Craft 3 using pluginfactory.io, and am unable to call services from the primary module class using `$this->orders->method()` as shown in [the documentation](https://docs.craftcms.com/v3/extend/services.html#calling-service-methods).
  
  The services appear to be registered correctly, as I'm able to call the service methods elsewhere in the module using the `SiteModule::$instance->orders->method()` format.
  
  The line `use modules\sitemodule\services\Orders as OrdersService` is at the top of the module class, but removing makes no difference in being able to call those service methods from the primary module class.
  
  What is the correct way to call service methods from a primary module class?
  
  **Tags:** craft3, modules
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2019-06-22. Score: 3 | Accepted answer
  
  Where do you instantiate your `orders` component?
  
  As long as you're calling the `setComponents` early in your `init` method as follow, you should be able to access it anywhere in your plugin class:
  
  ```php
  ...
  parent::init();
  self::$plugin = $this;
  $this->setComponents([
          'orders' => OrdersService::class,
  ]);
  ...
  
  ```
  
  > **Comments:**
  > **username_0**: Not sure how the services were getting registered before so that they worked in every other module file except the primary module class, but in any case this worked. Thanks for your help!  
  
  ---
  
  ### Answer by username_2 on 2022-02-11. Score: 1
  
  If this is a module, the `self::$plugin = $this;` won't work.
  
  You gotta register your component thru app.php
  
  ```
  use modules\sitemodule\Orders;
  use modules\sitemodule\services\OrdersService;
  
  return [
      'modules' => [
          'sitemodule' => [
              'class' => Orders::class,
              'components' => [
                  'ordersService' => OrdersService::class
              ]
          ]
      ],
      'bootstrap' => ['sitemodule']
  ];
  
  ```
  
  Now you can call it in your controller like...
  
  ```
  Orders::getInstance()->ordersService->doSomething();
  
  ```
  
  
  ---
  
  