Using Namespaces with an Autoloader

In the last few articles, we looked at autoloaders and ways to prevent collisions between the names of classes and their methods. In this article, we’ll see how to use the combination of namespaces with an autoloader.

 

MODX logo

 

Our Project

In this article, we’ll continue to use our “Pets” project from the previous articles. Here’s the directory structure:

[code language=”html”]
MODX_CORE_PATH components/pets
controllers
mycontroller.class.php
model
pets
dog.class.php
cat.class.php
[/code]

 

The Files

For reference, here are the class files from the previous article:

Dog class file:

[code language=”php”]
<?php
namespace Pets\Model\Pets;

class Dog {
function __construct() {

}
function speak() {
return ‘arf’;
}
}
[/code]

Cat class file:

[code language=”php”]
<?php
namespace Pets\Model\Pets;

class Cat {
function __construct() {

}
function speak() {
return ‘meow’;
}
}
[/code]

Controller class file:

[code language=”php”]
<?php
namespace pets\controllers;

class MyController {
function __construct() {
}
}

[/code]

 

Autoloader with Namespaces

When a registered autoloader is called in a file with a namespace specified at the top of the file, PHP sends the autoloader the namespace specification as a prefix to the class name. All our autoloader needs to do is translate that specification to a file path and include the file. Let’s look at our new autoloader code:

[code language=”php”]
<?php
function my_autoloader($class) {
// echo “\nInitial Class: ” . print_r($class, true);

$class = str_replace(“\\”, DIRECTORY_SEPARATOR , $class);
// echo “\nConverted Class: ” . $class;
$path = MODX_CORE_PATH . ‘components’ . DIRECTORY_SEPARATOR . $class . ‘.class.php’;
$path = strtolower($path);
// echo “\nFinal Path: ” . $path;

require_once($path);
}

spl_autoload_register(‘my_autoloader’);
[/code]

The commented lines are handy for debugging the process. If, for example, we uncomment them and use this code:

[code language=”php”]
<?php
namespace Pets;

include_once MODX_CORE_PATH . ‘/components/pets/autoload.php’;

$dog = new Model\Pets\Dog();
[/code]

The output produced inside our autoloader will look something like this:

[code language=”html”]
Initial Class: Pets\Model\Pets\Dog
Converted Class: Pets\Model\Pets\Dog
Path: c:/xampp/htdocs/addons/core/components\pets\model\pets\dog.class.php
[/code]

The first two lines show the same thing because in my versions of Windows and PHP, the directory separator is already a backslash, but it’s good form to make the conversion to make sure your code will work on all platforms.

Using Our Autoloader

The changes to our previous code are actually quite small (we’ll use our namespace aliases from the previous article). All we have to do is replace the include line with a line to include our autoloader:

[code language=”php”]
<?php
namespace Pets;

use Pets\Model\Pets as Classes;
use Pets\Controllers as Controllers;

include_once MODX_CORE_PATH . ‘/components/pets/autoload.php’;

$dog = new Classes\Dog();

if ($dog) {
echo “\nDogs say: ” . $dog->speak();
}

$cat = new Classes\Cat();
echo “\nCats say: ” . $cat->speak();

$controller = new Controllers\MyController();

if ($controller) {
echo “\nController: OK”;
}

[/code]

That’s all there is to it. We’ve specified the namespace in our class files and the file that uses the classes. Once that’s done, our autoloader will find the files automatically.

Modern Autoloaders

Our autoloader is still a little primitive, though it’s simple enough that it’s easy to understand. It can only be used for our project. The code would have to be changed for use in other projects.

Modern autoloaders are often classes themselves with methods that let you set the base path and have multiple, unrelated locations for the class files. Often, they use an anonymous PHP function rather than calling spl_autoload_register() with a regular function as an argument. Some are quite complex. There are a couple of relatively simple ones here.

One common autoloader comes with Composer and you can integrate it into a project, though it’s not trivial to do so unless the project itself is installed with Composer. Upcoming versions of MODX will very likely have a built-in autoloader that may be available for use in all our classes.

 

Coming Up

In the next article, we’ll move on to a new topic. A series of articles will explore how to bypass the MODX Manager Login, using your own code or someone else’s to authenticate the user.

 


For more information on how to use MODX to create a web site, see my web site Bob’s
Guides
, or
better yet, buy my book: MODX: The Official Guide.

Looking for quality MODX Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , | Posted under MODX, MODX | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray is the author of MODX: The Official Guide and over 30 MODX add-on components. He hosts Bob's Guides, a source of valuable information for MODX users, and has been very active in the MODX Forums with over 19,000 posts.

Leave a Reply

Your email address will not be published. Required fields are marked *