Answers.Body: <p>There's a lot going on here, but a) assuming you're really using Craft 3 and b) you generated your plugin, service and variable scaffolding on <a href="https://pluginfactory.io" rel="nofollow noreferrer">https://pluginfactory.io</a>, this should work:</p>
  
  <p><code>KGVariable.php</code> Variable</p>
  
  <pre><code>&lt;?php
  
  namespace kg\variables;
  
  class KGVariable
  {
      public function sayHello()
      {
          return \kg\KG::$plugin-&gt;useTR-&gt;exampleService();
      }
  }
  </code></pre>
  
  <p><code>UseTR.php</code> Service</p>
  
  <pre><code>&lt;?php
  
  namespace kg\services;
  
  use craft\base\Component;
  
  class UseTR extends Component
  {
      public function exampleService()
      {
          $result = 'hello';
          return $result;
      }
  }
  </code></pre>
  
  <p>Then using this in a template will return <code>hello</code>:</p>
  
  <pre><code>{{ craft.kG.sayHello() }}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-06T03:43:27.373Z
Answers.Text: It gives me the following error:
Answers.UserDisplayName: user8983
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-06T03:49:39.907Z
Answers.Text: Calling unknown method: craft\web\twig\variables\CraftVariable::keyword()
Answers.UserDisplayName: user8983
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-06T19:52:31.417Z
Answers.Text: You've got something going on somewhere else, then. The string `keyword` isn't in any of the code posted above or generated by pluginfactory.io.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-05T20:04:22.410Z
Answers.LastActivityDate: 2018-11-05T20:04:22.410Z
Answers.UserDisplayName: Brad Bell
Body: <p>I have a service called <code>useTR.php</code>. It has a public method <code>exampleService()</code>. </p>
  
  <p>In the variables folder I have added the following code in my <code>KGVariable.php</code> (KG is my plugin name)</p>
  
  <pre><code>public function sayHello() {
      return craft()-&gt;useTR-&gt;exampleService();
  }
  </code></pre>
  
  <p>In my templates folder I have an html that gets loaded after a form submission and that html has the following code. </p>
  
  <pre><code>&lt;p&gt;Thank you {{ craft.KG.sayHello() }} will contact you soon&lt;/p&gt;
  </code></pre>
  
  <p>When this HTML is loaded, I get the following error:</p>
  
  <blockquote>
    <p>Twig Runtime Error – Twig_Error_Runtime
    Calling unknown method: craft\web\twig\variables\CraftVariable::keyword()</p>
  </blockquote>
  
  <p>What am I doing wrong? Kindly help (I am a beginner at Craft). Thanks.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-11-04T19:44:10.943Z
Comments.Text: Do you use Craft 3 or Craft 2. The code is for Craft 2 but the exception for Craft 3. It's likely you are using the wrong code. Furthermore the exception is not really related to your code. You might want to take a look at pluginfactory.io - it creates the entire structure with example code for you
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-11-05T18:15:04.147Z
Comments.Text: I am using Craft 3. I created the entire structure using pluginfactory.io. Do you see any issue with the way I am writing the code, I mean any obvious issue.
Comments.UserDisplayName: user8983
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-11-05T18:26:16.127Z
Comments.Text: Yes it's for Craft 2 and not Craft 3. There is no `craft()` function in v3
Comments.UserDisplayName: Robin Schambach
Tags: <craft3>
Title: Plugin service and variable
ThreadText: # Plugin service and variable
  
  (Asked by: username_0 on 2018-11-04)
  
  I have a service called `useTR.php`. It has a public method `exampleService()`.
  
  In the variables folder I have added the following code in my `KGVariable.php` (KG is my plugin name)
  
  ```
  public function sayHello() {
      return craft()->useTR->exampleService();
  }
  
  ```
  
  In my templates folder I have an html that gets loaded after a form submission and that html has the following code.
  
  ```
  <p>Thank you {{ craft.KG.sayHello() }} will contact you soon</p>
  
  ```
  
  When this HTML is loaded, I get the following error:
  
  > Twig Runtime Error – Twig\_Error\_Runtime
  > Calling unknown method: craft\web\twig\variables\CraftVariable::keyword()
  
  What am I doing wrong? Kindly help (I am a beginner at Craft). Thanks.
  
  **Tags:** craft3
  
  > **Comments:**
  > **username_1**: Do you use Craft 3 or Craft 2. The code is for Craft 2 but the exception for Craft 3. It's likely you are using the wrong code. Furthermore the exception is not really related to your code. You might want to take a look at pluginfactory.io - it creates the entire structure with example code for you  
  > **username_0**: I am using Craft 3. I created the entire structure using pluginfactory.io. Do you see any issue with the way I am writing the code, I mean any obvious issue.  
  > **username_1**: Yes it's for Craft 2 and not Craft 3. There is no `craft()` function in v3  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2018-11-05. Score: 1
  
  There's a lot going on here, but a) assuming you're really using Craft 3 and b) you generated your plugin, service and variable scaffolding on <https://pluginfactory.io>, this should work:
  
  `KGVariable.php` Variable
  
  ```
  <?php
  
  namespace kg\variables;
  
  class KGVariable
  {
      public function sayHello()
      {
          return \kg\KG::$plugin->useTR->exampleService();
      }
  }
  
  ```
  
  `UseTR.php` Service
  
  ```
  <?php
  
  namespace kg\services;
  
  use craft\base\Component;
  
  class UseTR extends Component
  {
      public function exampleService()
      {
          $result = 'hello';
          return $result;
      }
  }
  
  ```
  
  Then using this in a template will return `hello`:
  
  ```
  {{ craft.kG.sayHello() }}
  
  ```
  
  > **Comments:**
  > **username_0**: It gives me the following error:  
  > **username_0**: Calling unknown method: craft\web\twig\variables\CraftVariable::keyword()  
  > **username_2**: You've got something going on somewhere else, then. The string `keyword` isn't in any of the code posted above or generated by pluginfactory.io.  
  
  ---
  
  