If you code clean and have clear separations of responsibility, at some point you'll need to include a new class in your component, plugin, or module. In PHP 5, there is an autoloader class within the Standard PHP Library. Autoloaders enable a developer to only include a file when it's needed and a class calls it. This saves in memory and performance.
In Joomla, instead of using the SPL autoloader, we use JLoader a decorator for the SPL autoloader. In addition to the normal autoloader behavior, JLoader adds some explicit loading and some behavior that makes it easy to import an entire library instead of just one file.
Here is a common way to load a class:
require_once(dirname(__FILE__) . '/MyClass.php');
Here is how we do it using JLoader:
JLoader::register('MyClass', JPATH_COMPONENT . '/classes/MyClass.php');
In the future, we'll look at some interesting things you can do with JLoader and Joomla that expand its value beyond performance savings.Last modified on