HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Autoloader class

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
autoloaderclassstackoverflow

Problem

Depending on your directory structure you may have name spaced class files in different directories (that isn't really part of the structure, i.e. lib/ or core/).

Symfony ClassLoader is awesome, but I wanted something lightweight. That just solves the problem above, so inspired by other frameworks implementations I did this class.

Yes, it uses PSR-0.

I'd like some improvements on how I can get a better structure, use best practises and just make the class loader get as good as it can be.

directories as $directory)
        {
            if (file_exists($path = $directory . DIRECTORY_SEPARATOR . $class))
            {
                require_once $path;

                return true;
            }
        }
    }

    public function register()
    {
        spl_autoload_register(array($this, 'loadClass'));
    }

    public function addDirectories($directories)
    {
        $this->directories = (array) $directories;
    }

    public function getDirectories()
    {
        return $this->directories;
    }
}


Example Use:

require_once __DIR__ . '/lib/MyPro/Autoloader.php';

$autoloader = new MyPro\Autoloader();
$autoloader->addDirectories(array(
    __DIR__ . '/controllers',
    __DIR__ . '/lib',
));
$autoloader->register();


Example Controller:

<?php
namespace Controller;

class Test extends MyPro\Controller
{
    public function index()
    {
       echo 'hello!';
    }
}


--- Is this correct use of namespaces (if you thinking this as some sort of framework)

Solution

One minor improvement would avoid an extra string concatenation while looping over the directories.

public function loadClass($class)
{
    $class = str_replace(array('\\', '_'), DIRECTORY_SEPARATOR, $class). '.php';

    if ($class[0] != DIRECTORY_SEPARATOR) {
        $class = DIRECTORY_SEPARATOR . $class;
    }

    foreach ($this->directories as $directory)
    {
        if (file_exists($path = $directory . $class))
        ...


Note that this assumes a single-character directory separator.

My only quibble with the class, and I understand you want something lightweight, is that there's no checking of the array passed to addDirectories. This shouldn't be too much of a problem since it will fail pretty quick if the parameter isn't an array or contains non-strings, but if that comes later during the execution the error will be harder to spot. I think you'll get a simple "class not found" error.

Oh, and the method should be named setDirectories since it removes any existing directories, or it should append them to the existing array.

Code Snippets

public function loadClass($class)
{
    $class = str_replace(array('\\', '_'), DIRECTORY_SEPARATOR, $class). '.php';

    if ($class[0] != DIRECTORY_SEPARATOR) {
        $class = DIRECTORY_SEPARATOR . $class;
    }

    foreach ($this->directories as $directory)
    {
        if (file_exists($path = $directory . $class))
        ...

Context

StackExchange Code Review Q#26327, answer score: 2

Revisions (0)

No revisions yet.