8

This is probably a really stupid question but I cannot find the answer and trial and error has failed.

I am developing a component for Joomla 3.x and I have a number of classes that I want to use. I am trying to implement the JLoader::registerPrefix() method. Lets say I have two classes Helper and NotHelpful. Helper is a static class stored at:

JPATH_LIBRARIES . '/foo/bar/Helper.php'

and NotHelpful is a non static class stored at:

JPATH_LIBRARIES . '/foo/baz/NotHelpful.php'

If I want to register these my understanding is I would call:

JLoader::registerPrefix('Foo', JPATH_LIBRARIES . '/foo'); 

which I am doing from the entry point to the application.

Then to call Helper, I would just use FooBarHelper::method1(); or to instantiate a new NotHelpful would be $Class = new FooBazNotHelpful();

What is unclear to me is what the actual class name inside of Helper.php or NotHelpful.php should be. Should it be FooBarHelper, Helper, BarHelper, or something else entirely?

When I try to call FooBarHelper::method1(); I get the error no matter what my class is named it seems:

PHP Fatal error: Class 'FooBarHelper' not found in

I am clearly doing at least one thing wrong. Can anyone tell me what one or more of them are?

1

My understanding of the prefix loader is that your filenames will ALWAYS be all lowercase.

This is good: JLoader::registerPrefix('Foo', JPATH_LIBRARIES . '/foo');

But then your class names translate to different class paths.

FooBarHelper => JPATH_LIBRARIES . '/foo/bar/helper.php'

FooBazNotHelpful => JPATH_LIBRARIES . '/foo/baz/not/helpful.php'

The prefix system always follows this convention, based on the doc here:

The class name must be in camel case and each segment of the name will represent a folder path where the last segment of the name is the name of the class file.

To answer your final question, the class name should be CamelCase, and it has to be in a file that has a lowercase name and is in folders that are all lowercase.

  • So in this example would the class definition within helpful.php be class Helpful { ... or class FooBazNotHelpful { ... or something else? – Andy Nov 23 '14 at 19:24
  • class FooBazNotHelpful @andy – David Fritsch Nov 23 '14 at 20:42
4

The schema that you're looking for is actually [ComponentName][Directory][Name]. Components generally have at least the following directories: controller, model, view, helpers. For a component called com_mycomponent with a helper file that is helper.php, that would be MycomponentHelperHelper. The camelcase formatting is particularly important in the class being properly recognized.

Unless your component is providing classes that you would want to make available to other components, you don't need to use JLoader::registerPrefix(). You can instead just use JLoader::discover(), and provide those classes to your component. for com_mycomponent/helpers this would mean

JLoader::discover('Helpers', JPATH_COMPONENT.'/helpers');

You can also use JLoader::registerNamespace() to use PHP namespaces.

See http://docs.joomla.org/Using_own_library_in_your_extensions for more details on those methods.

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.