Porto

SPL Autoload

SPL Autoload

  1. Abstract
  2. Loading a directory of classes
  3. Mulitple Autoloads
  4. Interfaces

Abstract

The SPL __autoload() method is one of the Magic Methods supplied in PHP. The __autoload method is called whenever a class is instantiated and will load the classs the the first time it is called. No longer is include(), require, include_once() or require_once needed as the SPL autoload takes care of this interally.

Loading a Directory of Classes

In its simplest form, the SPL autoload class can find all class files in a directory, where the class the class name, matches the file name. This is great for maintaining a naming convention through-out projects.

Assuming a class was defined as class Norman {..} then a simple namimg convention can be used to to assure the correct file is included, and the class contained within instantiated. In this example, a directory named classes contains 3 class files.

classes --+
          + mail.class.php
          + norman.class.php
          + db.class.php

To instantiate the norman class, or any other class within the classes directory, the SPL autoload method will load the class definition file without any need to include it.


<?php
    
/*** nullify any existing autoloads ***/
    
spl_autoload_register(nullfalse);

    
/*** specify extensions that may be loaded ***/
    
spl_autoload_extensions('.php, .class.php');

    
/*** class Loader ***/
    
function classLoader($class)
    {
        
$filename strtolower($class) . '.class.php';
        
$file ='classes/' $filename;
        if (!
file_exists($file))
        {
            return 
false;
        }
        include 
$file;
    }

    
/*** register the loader functions ***/
    
spl_autoload_register('classLoader');

    
/*** a new instance if norman ***/
    
$norman = new norman;

    
/*** make norman do something ***/
    
$norman->do_something();
?>

Note in the script above, that the file types are able to specified also, allowing a mixture of files to exist within the same directory that do not adhere to the naming convention. But of course, naming conventions are great to keep of exactly what files do. An application may have several repositories of files for differing purposes, such as controllers, views and libs.

Mulitple Autoloads

The SPL Autoload allows mulitple autoload functions to be registered. The same rules apply as above, and a second loader class is added and registered.


<?php

    
/*** nullify any existing autoloads ***/
    
spl_autoload_register(nullfalse);

    
/*** specify extensions that may be loaded ***/
    
spl_autoload_extensions('.php, .class.php, .lib.php');

    
/*** class Loader ***/
    
function classLoader($class)
    {
        
$filename strtolower($class) . '.class.php';
        
$file ='classes/' $filename;
        if (!
file_exists($file))
        {
            return 
false;
        }
        include 
$file;
    }

    function 
libLoader($class)
    {
        
$filename strtolower($class) . '.lib.php';
        
$file ='libs/' $filename;
        if (!
file_exists($file))
        {
            return 
false;
        }
        include 
$file;
    }

    
/*** register the loader functions ***/
    
spl_autoload_register('classLoader');
    
spl_autoload_register('libLoader');

    
/*** a new instance of norman ***/
    
$norman = new norman;

    
/*** make norman do some thing ***/
    
$norman->do_something();

In this second example, any files with the extension in the libs directory are also loaded. These files would have the extension of .lib.php to maintain a naming convention. Not only would the files be loaded from the calling script, but also from within the classes files that were themselves included.

Interfaces

Because the SPL __autoload function is so amazingly amazing, it will also load up interfaces which are implemented by other classes. In this example, the interface follows a naming convention much the same as with other classes. The class will be assumed to live in the classes directory as above.


<?php
    
/*
     * icontroller.class.php
     * interface to ensure all classes have an index method
     *
     */
    
interface iController
    
{
        public function 
index();
    }
?>


<?php
    
/*** nullify any existing autoloads ***/
    
spl_autoload_register(nullfalse);

    
/*** specify extensions that may be loaded ***/
    
spl_autoload_extensions('.php, .class.php');

    
/*** class Loader ***/
    
function classLoader($class)
    {
        
$filename strtolower($class) . '.class.php';
        
$file ='classes/' $filename;
        if (!
file_exists($file))
        {
            return 
false;
        }
        include 
$file;
    }

    
/*** register the loader functions ***/
    
spl_autoload_register('classLoader');

    class 
blog implements iController
    
{
        public function 
index()
        {
            echo 
'hello from the index';
        }
    }

    
/*** a new blog instance ***/
    
$blog = new blog;

    
/*** run the index method ***/
    
$blog->index();
?>

In this example, the SPL __autoload function has not only included the blog class, but also the icontroller class which the blog class extends from. This sort of behaviour almost obsoletes the need for require, include and friends.

By maintaining this structure, applications and websites developed can save on maintainence when new classes are added, as there is no need to be hunting through code to include them, its all done internally by SPL