<p>From what I understand about plugin development on Craft, this isn’t possible. The idea of the variables file is to provide methods which are accessible from within your plugin or users templates.</p>

<p>The idea being that the methods you define here are actually references to your service methods (<a href="http://buildwithcraft.com/docs/plugins/variables" rel="nofollow">Taken from Craft docs</a>) </p>

<blockquote>
  <p>Many of your template variable methods will likely be simple wrappers for your service API methods. It’s important to remember not to expose state-changing methods, however.</p>
</blockquote>

<p>So from that I would actually create a service class for your plugin and access the methods in your getInputHtml function from that.</p>

<p>For example:</p>

<p><strong>services/MyPlugin_PluginService.php</strong></p>

<pre><code>&lt;?php
namespace Craft;

class MyPlugin_PluginService extends BaseApplicationComponent
{
 public function helloWorld()
 {
   return ‘Hello World’;    
  }
}
</code></pre>

<p><strong>In your getInputHtml function</strong></p>

<pre><code>$testing = craft()-&gt;myPlugin_plugin-&gt;helloWorld();
</code></pre>

<h2>Call the same method in your templates</h2>

<p>Add this to your <strong>variables/MyPluginVariable.php</strong> file to call the method in your templates:</p>

<pre><code>public function sayHello() {
  return craft()-&gt;myPlugin_plugin-&gt;helloWorld();
}
</code></pre>

<p>Then you could call the service method in your templates like so:</p>

<pre><code>{{ craft.myPlugin.sayHello() }}
</code></pre>

<p>Hope that helps!</p>

CC BY-SA 3.0
2014-10-21T14:01:14.480Z
This works perfectly, thank you Alec! I'm still trying to familiarize myself with the way plugins are structured, and this is just another scenario where I overlook areas I shouldn't be.

The idea was to have a function that sits somewhere, which I can call from the frontend AND from within a plugin. Wonder if I can call the service plugin from the frontend too? {{ craft.myPlugin_plugin.helloWorld() }}. Will look into it properly this afternoon. Thanks again.
Mark Notton
CC BY-SA 3.0
2014-10-21T15:10:29.933Z
No worries, it took me a while to get my head around it to. I've updated my answer to show how you would call the same method in your templates :)
Alec Ritson
CC BY-SA 3.0
2014-10-21T11:34:29.390Z
2014-10-21T15:10:05.913Z
2014-10-21T15:10:05.913Z
Alec Ritson
<p>I have a plugin called "myPlugin", this has a variables file. Within there, I have a something like this:</p>

<pre><code>public function helloWorld() { return "Hello World"; }
</code></pre>

<p>Also within this plugin, I've created a new fieldtype. From the "getInputHtml" function, I'd like to call the "helloWorld()" function. </p>

<p>How can I do this? This was my initial trail of thought:</p>

<pre><code>$testing = craft()-&gt;myPlugin-&gt;helloWorld();
</code></pre>

<p>or </p>

<pre><code>$testing = craft()-&gt;myPlugin-&gt;myPluginVariable-&gt;helloWorld();
</code></pre>

<plugin-development><function>
Call a public function within a FieldType file, from a variables files
# Call a public function within a FieldType file, from a variables files

(Asked by: username_0 on 2014-10-21)

I have a plugin called "myPlugin", this has a variables file. Within there, I have a something like this:
public function helloWorld() { return "Hello World"; }

Also within this plugin, I've created a new fieldtype. From the "getInputHtml" function, I'd like to call the "helloWorld()" function. 
How can I do this? This was my initial trail of thought:
$testing = craft()->myPlugin->helloWorld();

or 
$testing = craft()->myPlugin->myPluginVariable->helloWorld();

**Tags:** plugin-development, function

---

## 1 answers

### Answer by username_1 on 2014-10-21. Score: 4 | Accepted answer

From what I understand about plugin development on Craft, this isn’t possible. The idea of the variables file is to provide methods which are accessible from within your plugin or users templates.
The idea being that the methods you define here are actually references to your service methods (Taken from Craft docs) 

Many of your template variable methods will likely be simple wrappers for your service API methods. It’s important to remember not to expose state-changing methods, however.

So from that I would actually create a service class for your plugin and access the methods in your getInputHtml function from that.
For example:
services/MyPlugin_PluginService.php

In your getInputHtml function
$testing = craft()->myPlugin_plugin->helloWorld();

Call the same method in your templates
Add this to your variables/MyPluginVariable.php file to call the method in your templates:
public function sayHello() {
  return craft()->myPlugin_plugin->helloWorld();
}

Then you could call the service method in your templates like so:
{{ craft.myPlugin.sayHello() }}

Hope that helps!

> **Comments:**
> **username_0**: This works perfectly, thank you Alec! I'm still trying to familiarize myself with the way plugins are structured, and this is just another scenario where I overlook areas I shouldn't be.  The idea was to have a function that sits somewhere, which I can call from the frontend AND from within a plugin. Wonder if I can call the service plugin from the frontend too? {{ craft.myPlugin_plugin.helloWorld() }}. Will look into it properly this afternoon. Thanks again.  
> **username_1**: No worries, it took me a while to get my head around it to. I've updated my answer to show how you would call the same method in your templates :)  

---

