phpautoloadspl-autoloader

PHP - Autoload only needed classes


I have an autoloader that is placed as a php file above all other sub directories in my project.

What it does is it loads all possible classes at once for any specific server request. After further thought I concluded I need to autoload only the required classes.

What do I need to do to avoid loading other classes not needed?
If I need to post the relevant code snippets of the class files in my subdirectories, I can.

<?php
    namespace autoloader;
    class autoloader
    {
        private $directoryName;
        
        public function __construct($directoryName)  
        {
            $this->directoryName = $directoryName;
        }  
        
        public function autoload()
        {
            foreach (glob("{$this->directoryName}/*.class.php") as $filename)
            {
                include_once $filename;           
            }
         
            foreach (glob("{$this->directoryName}/*.php") as $filename)
            {
                include_once $filename;           
            }
        }    
    }
    
    # nullify any existing autoloads
    spl_autoload_register(null, false);

    # instantiate the autoloader object
    $classes = [
        new autoloader('request'),
        new autoloader('config'),
        new autoloader('controllers'), 
        new autoloader('models'),
        new autoloader('data')
    ];

    # register the loader functions
    foreach ($classes as $class)
        spl_autoload_register(array($class, 'autoload'));

Solution

  • All registered autoloader functions will be called when you try to instantiate a new class or until it finally loads the class or throws an error. The way you have it now, you're registering the same autoloader function again and again for each directory, and file.

    What you'd want to do is something along the lines of this.

    namespace autoloader;
    
    class autoloader
    {
        public function __construct()
        {
            spl_autoload_register([$this, 'autoload']);
        }
    
        public function autoload($classname)
        {
            if (! file_exists("{$classname}.class.php")) {
                return;
            }
    
            include_once "{$classname}.class.php";
        }    
    }
    
    new autoloader();
    

    Every autoloader function gets the class FQCN passed into it, and from there you'll have to parse it and figure out if you can load the file where that class exists. For instance, if I do the following.

    use Some\Awesome\ClassFile;
    
    $class = new ClassFile();
    

    The autoloader we've registered will get the string Some\Awesome\ClassFile passed in as an argument, which we can then parse and see if we have a file for that class, if we don't we return out of the function and let the next registered autoloader function try and find the class.

    You can read more about autoloaders in the documentation, I also wrote a blog post about it like 2 months ago that might interest you.