7

I have created a helper file test.php as following:

administrator/components/com_compname/helpers/test.php

class TestHelper{
   //class methods
}

In controllers/subcontrollername.php We can use either of them:

1) require_once(JPATH_COMPONENT.DIRECTORY_SEPARATOR.'helpers'.DIRECTORY_SEPARATOR.'test.php' );

2) JLoader::register('TestHelper','/path/to/test.php');

But I am wondering which of the two is appropriate and why or does this even matter or not. Can anyone please guide me on this.

1 Answer 1

10

Actually JLoader::register('TestHelper','/path/to/test.php'); dosen't load file, it just registers it to be able to autoload file if class is instantiated.

Do require it instantly, you want to use:

JLoader::import('TestHelper','/path/to/test.php');

Note: for this your file name should be TestHelper.php.

which will

  1. include_once the file if it exists (with require_once you would get PHP error, if it dosen't exist. Now you would get pretty Joomla! error).

  2. Will remember that this file is already loaded and wont include_once it again. This results in minor performance gain.

which won't

  1. JLoader::register() your class, so you can't use JLoader::load() later.

All in all:

  1. Minor performance gain.
  2. It's good practice to follow coding style of framework (though Joomla! dosen't do it everywhere itself).
  3. You can use dot notation for class name (not applicable for *Helper classes.).

Dot notation example

jimport('sub.path.classname','/path/to/')

would include_once file in /path/to/sub/path/classname.php

NOTE

JLoader::import('TestHelper','/path/to/test.php')

has shorthand

jimport('TestHelper','/path/to/test.php')

6
  • Thank you for a very nice explanation. But I would need your help a little further. I used JLoader::import('TestHelper','/path/to/test.php'); but it gives me an error 'TestHelper class not found' . I did the following after importing the helper file: TestHelper::getTestfunction() . But it does not work
    – Liz.
    Feb 3, 2016 at 9:32
  • 1
    Is your /path/to without .php? Path has to be absolute. So in your case JLoader::import('TestHelper', JPATH_COMPONENT.DIRECTORY_SEPARATOR.'helpers');
    – Rene Korss
    Feb 3, 2016 at 9:34
  • no it was with php. JLoader::import('TestHelper',JPATH_COMPONENT.DIRECTORY_SEPARATOR.'helpers'.DIRECTORY_SEPARATOR.'test.php'); But I also tried without .php. Still it doen't work
    – Liz.
    Feb 3, 2016 at 9:43
  • You file should be named TestHelper.php.
    – Rene Korss
    Feb 3, 2016 at 10:59
  • 5
    Just to add to this, JLoader::register() actually registers the class to the autoloader using a given path. So later when you want to use the class you don't need a second import statement of any kind, PHP will load it automagically through the autoloading mechanisms of JLoader after that initial call to the register method.
    – Michael
    Feb 4, 2016 at 23:22

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.