4

Back in the Joomla 1.6/1.7 days, my understanding was that when you wanted to use the filesystem API, for example:

JFolder::create(...);

You had to import the Filesystem using jimport, like so:

jimport('joomla.filesystem.folder');

With Joomla 3.x, is this even required?

I could be wrong, but I thought this was only ever required for older versions of Joomla.

9

Most of the classes in Joomla 3.x are autoloaded. JFolder and JFile are one of the few exceptions. The reason is because they don't follow the naming convention for the autoloader.

However there is a nicer way than using the jimport statements. You can tell the autoloader where to find these classes. I do this in my component entry point:

// Joomla doesn't autoload JFile and JFolder
JLoader::register('JFile', JPATH_LIBRARIES . '/joomla/filesystem/file.php');
JLoader::register('JFolder', JPATH_LIBRARIES . '/joomla/filesystem/folder.php');

After that, the classes are autoloaded just fine.

4

jimport was a mistake that should have never been in the system in the first place (personal opinion). Someone thought "Cool! Looks like java!" and we got saddled with it. It was merely an extra layer of code wrapped around JLoader::import in the first place. (I dunno, maybe someone's editor didn't have autocomplete or global search and replace and they thought typing 8 characters less was worth adding an extra call and context switch on the stack every time a file was loaded.)

Back in prehistoric times PHP didn't have a mechanism for loading classes, so something that loaded them was necessary. These days PHP has a built-in mechanism for setting up an autoloader for your projects. And yes, there's an autoloader in Joomla, came in with some of the early platform code. There may be the occasional core class that requires it (I haven't tested them all, so I won't declare that none exist, just that very few of them do, if any at all) and some extensions that were written according to the old rules and haven't been edited to catch up with 21st-century coding standards may require it, but that's about it.

I know I've written several custom components and modules and haven't needed it once. My advice would be to not use it unless something complains, or unless you're writing something that needs to support very old versions of Joomla. It's a waste of bytes.

  • 1
    Fair point of view, but it does not really answer the question. – Valentin Despa Jun 18 '15 at 20:46
  • Read the second paragraph again (starting with the sentence "There may be an occasional class...") and maybe you'll change your mind. But I agree in that I gave far more information than was asked for, so you have to work a bit to get to the answer. It wasn't as succinct as, "No" would have been, but hopefully it explained why neither "No" nor "Yes" would have been 100% correct. The default position is "No" and only include it if something old and/or badly-written in the system complains. – Arlen Jun 23 '15 at 15:40
2

This is a late response but I think it can help to understand.

jimport is the first loader (if we can tell) of Joomla 1.7 to 3.0.

Here is the definition of the function taken in Joomla 4:

File: \libraries\loader.php

/**
 * Intelligent file importer.
 *
 * @param   string  $path  A dot syntax path.
 * @param   string  $base  Search this directory for the class.
 *
 * @return  boolean  True on success.
 *
 * @since       1.7.0
 * @deprecated  5.0   Classes should be autoloaded.
                      Use JLoader::registerPrefix()
                      or JLoader::registerNamespace() to register an autoloader for
 *                    your files.
 */
function jimport($path, $base = null)
{
    return JLoader::import($path, $base);
}

So you can use jimport until Joomla 5 without problem.

To respond to the question, it's required. If in the version you develop something doesn't work, it's because some classes like JFolder are not loaded (include() or require(), the classic/old way to load a class on older versions) because there is no need in memory if it is not used.

So first use JLoader on 3.0+ for future compatibility and use old jimport only on older versions 1.7-3.0 if you can't update to the latest for some reason.

As say in the documentation for Joomla 3+ : Use JLoader::registerPrefix() or JLoader::registerNamespace() to register an autoloader for your files.

JLoader::register() works but it is also deprecated.

It is useful also to register your own libraries.

0

You still need to import it, otherwise you will get fatal error something like this:

Fatal error: Class 'JFolder' not found in ......./path on line 15

  • Odd, considering I have used JFolder multiple times, and not had any errors – Lodder Jun 16 '15 at 13:18
  • @Lodder It was probably loaded already earlier in the cycle somewhere. – Bakual Jun 16 '15 at 14:17

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.