<p>I suppose it depends on what the code is going to be doing.  If it's standard "business logic" code related to your plugin, then it would make more sense to add it to your plugin's <a href="http://buildwithcraft.com/docs/plugins/services" rel="nofollow">services</a>, which is where we recommend that type of logic goes.  There, it handles the interactions with the database and returns the appropriate <a href="http://buildwithcraft.com/docs/plugins/models" rel="nofollow">models</a> back to your plugin's <a href="http://buildwithcraft.com/docs/plugins/controllers" rel="nofollow">controllers</a> and/or <a href="http://buildwithcraft.com/docs/plugins/variables" rel="nofollow">template variables</a>.</p>

<p>Adding it to your service layer will also get you the added benefit of Craft auto-uploading the classes for you as well and attaching them to the global <code>craft()</code> instance as <code>craft()-&gt;pluginHandle</code></p>

<p><strong>Update</strong></p>

<p>If we're talking about enums, I'd follow Craft's example and create an <code>enums</code> folder for your plugin.  From your plugin's <code>init()</code> method, simply import your custom enums:</p>

<pre><code>public function init()
{
    Craft::import('plugins.yourPluginHandle.enums.MyCustomEnum');
}
</code></pre>

CC BY-SA 3.0
2014-07-15T02:36:32.470Z
I might have to edit that to make it a little more clear... I am including my custom enum.
Jason McCallister
CC BY-SA 3.0
2014-07-15T03:31:50.190Z
Can you edit your original question to clarify?
Brad Bell
CC BY-SA 3.0
2014-07-15T03:33:53.553Z
I edited the title and content to make more sense. Thanks Brad.
Jason McCallister
CC BY-SA 3.0
2014-07-15T03:47:19.863Z
Are there any benefits to importing the enums in the `init()` method?
Jason McCallister
CC BY-SA 3.0
2014-07-15T04:16:37.470Z
Not particularly, just seems cleaner/more explicit to me.  If you load outside of init(), they'll they included as soon as the plugin file gets loaded.  Inside init, it'll be slightly later when the plugin's init() method gets called.
Brad Bell
CC BY-SA 3.0
2014-07-15T04:54:46.207Z
@Brad, does this method work if you are importing a library with a different namespace?
Ben Parizek
CC BY-SA 3.0
2014-07-15T04:58:06.867Z
@BenParizek If you're importing a whole separate, namespaced library, you should probably be doing that from Composer, anyway. :)
Brad Bell
CC BY-SA 3.0
2014-07-15T02:34:47.653Z
2014-07-15T03:38:34.137Z
2014-07-15T03:38:34.137Z
Brad Bell
<p>If I was going to include my custom enums for my plugin, should the include be outside of my plugin class or use the <strong>init()</strong> method. What is the best way to handle this and are there any pros or cons to either? </p>

<p><strong>Current method:</strong></p>

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

// include enums for custom statuses
include(dirname(__FILE__) . '/enums/PluginNameEnums.php');

class PluginNamePlugin extends BasePlugin
{
    // etc
}
</code></pre>

<plugin-development><best-practice>
How should I include my custom enums class in my plugin?
# How should I include my custom enums class in my plugin?

(Asked by: username_0 on 2014-07-15)

If I was going to include my custom enums for my plugin, should the include be outside of my plugin class or use the init() method. What is the best way to handle this and are there any pros or cons to either? 
Current method:

**Tags:** plugin-development, best-practice


---

## 1 answers

### Answer by username_1 on 2014-07-15. Score: 3 | Accepted answer

I suppose it depends on what the code is going to be doing.  If it's standard "business logic" code related to your plugin, then it would make more sense to add it to your plugin's services, which is where we recommend that type of logic goes.  There, it handles the interactions with the database and returns the appropriate models back to your plugin's controllers and/or template variables.
Adding it to your service layer will also get you the added benefit of Craft auto-uploading the classes for you as well and attaching them to the global craft() instance as craft()->pluginHandle
Update
If we're talking about enums, I'd follow Craft's example and create an enums folder for your plugin.  From your plugin's init() method, simply import your custom enums:
public function init()
{
    Craft::import('plugins.yourPluginHandle.enums.MyCustomEnum');
}

> **Comments:**
> **username_0**: I might have to edit that to make it a little more clear... I am including my custom enum.  
> **username_1**: Can you edit your original question to clarify?  
> **username_0**: I edited the title and content to make more sense. Thanks Brad.  
> **username_0**: Are there any benefits to importing the enums in the `init()` method?  
> **username_1**: Not particularly, just seems cleaner/more explicit to me.  If you load outside of init(), they'll they included as soon as the plugin file gets loaded.  Inside init, it'll be slightly later when the plugin's init() method gets called.  
> **username_2**: @Brad, does this method work if you are importing a library with a different namespace?  
> **username_1**: @BenParizek If you're importing a whole separate, namespaced library, you should probably be doing that from Composer, anyway. :)  

---

