JLoader Introduction

by in Development

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
Tagged in: jloader php
John is the owner and lead developer of Blue Bridge.  He's contributed to the Joomla project as a JCM author, JUG team leader, and a bug squasher.  In his spare time, he loves to write and read, advance his development and business knowledge, play video games, boulder, and spend time with his girlfriend and their dog Sunny.
blog comments powered by Disqus