2

I've created a custom component that (in addition to all the other component files) has a helper file located in:

/administrator/components/com_kr_books/helpers/kr_books.php

It has an abstract class called Kr_booksHelper, within which I have a number of processing functions.

I have created a 'stand alone' module that needs to access some functions from this helper file.

When just running a call to one of the functions I get:

0 Call to undefined method Kr_booksHelper::thisFunction()

Which makes sense, if there is no connection between the 2 files.

I've tried using the following to try to include the component helper file within the module's helper file:

JLoader::import('Kr_booksHelper','/administrator/components/com_kr_books/helpers/kr_books.php');

However, I'm still getting the 'undefined method'. And I've also tried all sorts of different syntax for the path to the file, but get the same results).

I've also tried:

require_once '../administrator/components/com_kr_books/helpers/kr_books.php';

However, then the site just returns a blank white screen.

Any ideas how I should properly access the functions within the component's helper file from my stand-alone module?

2 Answers 2

2

If you're getting undefined method error, it means your class is found but you're calling a non-existing method.

JLoader::import() first argument accepts dot-separated path with last element used as class name and second argument as base path. This is not suitable in your case because class name doesn't match its filename. Instead you can use JLoader::register() which accepts class name as first argument and its path file as second argument:

JLoader::register('Kr_booksHelper', JPATH_ADMINISTRATOR . '/components/com_kr_books/helpers/kr_books.php');

As a good practice you should use absolute paths to include files. In Joomla you can use JPATH_* constants like in example above.

When developing you should have error reporting enabled. Then you'd see this error in your require_once use:

Fatal error: require_once(): Failed opening required '../administrator/components/com_kr_books/helpers/kr_books.php'

When using relative scripts, PHP will look first in its configure include paths and then for paths relative to the script. So this would work but it's slow and not recommended:

require_once 'administrator/components/com_kr_books/helpers/kr_books.php';
1

Thanks for pointing me in the write direction. The problem was between my keyboard and my chair.

After staring at the code and seeing the function definition clearly there in the admin helper, and then seeing that a call to a different function from this component was working it made me realize that the helper file/class was already there, but missing the specific function.

Then I remembered that I have the same class in a helper file for the component on the front-end. So, even though I was directly loading the admin helper file, because the class was already loaded (because the component was active on the site) the class was already loaded, from the front-end helper file, which didn't have the function added to it yet (only added to the admin helper file).

Once I updated the front-end helper file with the new function, all works well.

So, the problem was caused by two errors:

  • Having a class with the same name for front-end and admin, but not keeping those class definitions up-to-date
  • When the class is loaded in one area (component, module, etc), it's there and available for other areas.

Thanks again for the clear explanation and pointing in the right direction to find the solution.

Your Answer

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

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