1

I have 2 or 3 custom plugins that I'm using on a project. In each file I have a number of 'helper' functions (is this the correct terminology?) that perform a task that might be required in a few of the functions within each plugin.

At the moment I have a copy of each helper function in each of the plugins. The code is the same and I realise this isn't the right way to go. My question is, where to put them and how to reference them from within each of these custom plugins?

Thanks in advance!

Jim

2 Answers 2

5

I would suggest bundling all of your custom functionality for the project into one add-on (more on this thinking) - then you can use EE's helper implementation for this. (Have a look at the recommended add-on directory structure.)

In your add-on package, add a helpers directory, and in it place one or more PHP files which contain your function(s). Be sure that they are all named with a _helper.php suffix.

my_addon
    helpers
        my_functions_helper.php
    mod.my_addon.php
    pi.my_addon.php
    (etc)

Then in your plugin or module:

$this->EE->load->helper('my_functions');
// you can now use all of the functions contained in my_functions_helper.php

But, if you want to stick with multiple add-ons, you can certainly include any shared file that lives inside your third_party directory:

include(PATH_THIRD.'/my_addon/libraries/my_library.php');

(The location and file naming after PATH_THIRD is up to you.)

4
  • Loving that answer Derek. So if I understand you correctly, $this->EE->load->helper('my_functions'); will only find my_functions_helper.php if it is part of the same addon? As I have 4 or 5 plugins, I may be better off going the 'include' method. Jan 16, 2013 at 13:37
  • Correct - only if it's part of the same add-on (or if the helper is located in the system-wide helpers directory - but it's not advisable to add to that). Jan 16, 2013 at 13:43
  • 2
    I would not include library, but actually load it using code like this: $this->EE->load->add_package_path(PATH_THIRD.'my_addon/'); $this->EE->load->library('my_library'); $result = $this->EE->my_library->some_function(); $this->EE->load->remove_package_path(PATH_THIRD.'my_addon/');
    – Yuri Salimovskiy
    Jan 16, 2013 at 13:44
  • Yuri - I like that, but I also don't ... it's messier, and it needs to happen every time you want to call one of the shared functions. What's the advantage in this case over the include()? Jan 16, 2013 at 15:22
2

It seems like you'll want to use a separate directory in the system/expressionengine/third_party/ hierarchy just for your library.

So if your plugins are plug1 and plug2, they'll normally live in system/expressionengine/third_part/plug1/pi.plug1.php and system/expressionengine/third_party/plug2/pi.plug2.php. For a common library referenced in both plugins, just put it somewhere like system/expressionengine/third_party/plugs/pluglib.php.

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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