Autoloading Classes in PHP

One of the challenges you’ll often encounter in development is how to structure your application and how to keep your code organized in such a way that that it is both maintainable and possesses the ability to scale well. This is especially important if you are building a framework of some kind. There are many different PHP frameworks out there. For example, frameworks such as CodeIgniter and Laravel are PHP frameworks that follow the Model View Controller (MVC) pattern (although their respective implementations of it are quite a bit different). If you were to sit down to build something similar to one of these PHP frameworks, you would need to determine how your application would load up the files and resources it needed based on the user’s request. Autoloading in PHP can be a useful feature to automatically load your class PHP files if they have not yet been included. This is an especially useful feature if you are building a framework of some sort that will involve user named and user created PHP files (as MVC frameworks often do). Users have to create their own Model, View, and Controller files corresponding to the type of application they are building, but your framework is going to have to figure out how to include instantiate these classes. How can you do this when the names of the files will be dynamic? This is where autoloading can help.

If the index.php file were the entry point for your application, you might think we’d have to do something like the following before doing anything else in our application…

require_once('classes/MyClass1.php');
require_once('classes/MyClass2.php');
require_once('classes/MyClass3.php');
require_once('classes/MyClass4.php');
require_once('includes/functions.php');

This sort of scenario can lead to difficulties with dependency. If one class depends on another, if your application ever changes, you have to work extra hard to be sure that the relationships between your classes are maintainable. But with autoloading classes in PHP this may not be necessary.

How would we do this?

In the past the __autoload function was used. Say we had a file MyClass.php…

<?php
class MyClass {
    public function __construct() {
        echo "MyClass initialized successfully...";
    }
}
?>

But we haven’t included this class anywhere yet with the include or require functions in PHP. And then in another file later on (e.g. index.php) we have the following…

<?php
function __autoload($classname) {
    $filename = "./". $classname .".php";
    include_once($filename);
}

// we've called a class ***
$obj = new MyClass();
?>

Amazingly, our class will work and get initialized. This is because the autoload function will load up or classes for us, which can be very handy. However, the spi_autoload_register function is now recommended as the new way of handling autoloading.

<?php

function my_autoloader($class) {
    include 'classes/' . $class . '.class.php';
}

spl_autoload_register('my_autoloader');
?>

Or, as of PHP 5.3.0, we can also use an anonymous function…

spl_autoload_register(function ($class) {
    include 'classes/' . $class . '.class.php';
});

Having multiple autoload functions can be useful if you want to have different “types” of PHP files loaded. Files containing classes may not the only type of PHP file present within your application. And of course, you’ll still have to manage your dependencies between classes if any of your classes rely on other classes to function properly. But at least autoloading will take care of the task of loading up your classes as you need them in your PHP application.

, 9bit Studios E-Books

Like this post? How about a share?

Stay Updated with the 9bit Studios Newsletter

0 Responses to Autoloading Classes in PHP

    Leave a Reply

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