4

I'm just getting acquainted with Add-on development beyond basic plugins and I'm not exactly sure the proper way to organize my files.

I have a base Module file, mod.xxxx.php that has a handful of methods within. I am now realizing I need an extension as well to hook into a few base EE hooks. Within the extension, I want to use a few helper methods from my module but I'm not sure the proper way to share them between the Module and Extension.

I tried

if (class_exists('Modulename') === false)
{
    require_once PATH_THIRD.'modulename/mod.modulename.php';
}
$modulename = new Modulename();

And then called

$modulename->modulemethod($variable);

In the extension, but it's not returning the right type, rather an empty array.

Should these "helper" functions really be moved into a helper file and then loaded into both the extension and the module using the load->helper function of CI?

1
  • I think I figured the empty array part out... I was setting a variable inside the called method based on the value of a frontend tag parameter. I think the question is still valid though, is there a best practice for organizing shared functions between modules and extensions?
    – noahkuhn
    Apr 26, 2014 at 19:39

1 Answer 1

4

You should create a library, which is just a PHP class stored in system/expressionengine/third_party/your_addon_name/libraries/.

<?php

class Your_library {
  public function do_something()
  {
     return 'foo';
  }
}

Then in your module / extension, you'd be able to do this:

ee()->load->library('your_library');

// prints out foo
echo ee()->your_library->do_something();

This is the preferred way to share code between the various add-on types.

1
  • Thanks Rob, this is what I was leaning towards, but I appreciate the confirmation. Cheers.
    – noahkuhn
    Apr 27, 2014 at 18:40

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.