Autoloading 101

For some who are new to PHP 5 you may have heard about a PHP feature called SPL Autoload or Autoloading classes.

Ordinarily, when you write a class your code may look something like this:

<?php

require_once('../code/foo.php');
require_once('../code/baz.php');

class widget extends foo
{
    public function event()
    {
        $baz = new baz();
    }
}

Autoloading does away with the need to put require / include once to include class file by enabling you to attach a function that performs an action before a class is instantiated. SPL autoload works when you try to instantiate a class or use reflection on a class and only works when the class hasn’t already been loaded.

Continue reading