PHP Autoloader - For classes, traits and interfaces Please review my Autoloader, which is the first file included in index.php

Note that there is no resource with the same name for trait, interface and class. Each resource has it's own name. And even if some class is named "Ha" and some trait is named "Ha", one of them has specific namespace for this.

&lt;?php
### Autoload class ###
class Autoload
{
    # The path for classes, traits or interfaces #
    private static $path  = 'private/Model/';

    # The resource files by default will be NULL #
    private static $resource_files = null;

    # Search for resource_files in recursive mode #
    private static function recursive_glob($pattern)
    {
        $resource_files = glob($pattern);
        foreach(glob(dirname($pattern).'/*', GLOB_ONLYDIR | GLOB_NOSORT) as $directory)
        {
            $resource_files = array_merge(
                $resource_files,
                self::recursive_glob($directory.'/'.basename($pattern))
            );
        }
        return $resource_files;
    }

    # Order array to start with files that has the name like the $resource or closely... #
    private static function order_resource_files($resource)
    {
        $resource_name = explode('\\', $resource);
        $resource_name = end($resource_name);

        foreach(self::$resource_files as $file_key =&gt; $file)
        {
            if(stripos($file, $resource_name) !== false)
            {
                self::$resource_files[-1] = $file;
                unset(self::$resource_files[$file_key]);

                ksort(self::$resource_files);
                self::$resource_files = array_values(self::$resource_files);
            }
        }
    }

    # Find resource #
    public static function find($resource)
    {
        # Set $resource_files if is NULL #
        if(!self::$resource_files)
        {
            self::$resource_files = self::recursive_glob(self::$path.'*.php');
        }

        # Order $resource_files by given resource #
        self::order_resource_files($resource);

        # Now that we have the $resource_files array, we can start searching #
        foreach(self::$resource_files as $file_key =&gt; $file)
        {
            # First, remove the file from array #
            unset(self::$resource_files[$file_key]);

            # Include file #
            require_once $file;

            # If resource exists now, then we can stop and search for other resources later #
            if(class_exists($resource) || trait_exists($resource) || interface_exists($resource))
            {
                break;
            }
        }
    }
}

### Check PHP version and call Autoload::find ###
if(version_compare(PHP_VERSION, '5.1.2', '&gt;='))
{
    if(version_compare(PHP_VERSION, '5.3.0', '&gt;='))
    {
        spl_autoload_register('Autoload::find', true, true);
    }
    else
    {
        spl_autoload_register('Autoload::find');
    }
}
else
{
    function __autoload($resource)
    {
        Autoload::find($resource);
    }
}
First of all you should use static methods and properties only
when there is good reason to do so. Generally you should default to non-static.


$path property can be const because private static doesn't say whether it is immutable or not.
I think you shouldn't order resource files at all. You already have resource name. All you should do is match something like $resource.'.' and include file if such exists. Also if you did require_once i don't see the need to check for class_exists() etc.
I have never written autoloader but my instinct tells me that if you failed to find resource file then you should throw exception.
Altering array inside foreach is murky and its good to avoid such practices.


PS: Personally I haven't seen any php project/library utilizing hashtag comments. It might be my OCD but #...# draws all my focus so my suggestion is to use either: // for line comments and /*...*/ for block comments.

Update:
What I had in mind when writing point 2 is something like this:

    foreach(self::$resource_files as $file_key =&gt; $file)
    {
        // Check if resource matches file name
        if (strpos($file, $resource.'.') !== false ) {
            unset(self::$resource_files[$file_key]);

            require_once $file;
        }
    }