4

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:

<?php
namespace Craft;

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

class PluginNamePlugin extends BasePlugin
{
    // etc
}
3

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');
}
  • I might have to edit that to make it a little more clear... I am including my custom enum. – Jason McCallister Jul 15 '14 at 2:36
  • Can you edit your original question to clarify? – Brad Bell Jul 15 '14 at 3:31
  • I edited the title and content to make more sense. Thanks Brad. – Jason McCallister Jul 15 '14 at 3:33
  • Are there any benefits to importing the enums in the init() method? – Jason McCallister Jul 15 '14 at 3:47
  • 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 Jul 15 '14 at 4:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.