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 class 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 internally.

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.

To instantiate the 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(null, false);

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

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

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

But from php version 5.1.2 or greater you can as follow:

class Autoloader {

    static public function loader($class_Name) {
        $className = strtolower($class_Name);
        $filename = path . str_replace('\\', '/', $className) . '.php';
        if (file_exists($filename) && is_readable($filename)) {
            include($filename);
            if (class_exists($className)) {
                return true;
            }
        }
        return false;
    }

}

if (version_compare(PHP_VERSION, '5.1.2', '>=')) {
    //SPL autoloading was introduced in PHP 5.1.2
    if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
        spl_autoload_register('Autoloader::loader', true, true);
    } else {
        spl_autoload_register('Autoloader::loader');
    }
} else {

    /**
     * Fall back to traditional autoload for old PHP versions
     * @param string $classname The name of the class to load
     */
    function __autoload($classname) {
        $className = strtolower($classname);
        $path = path . "{$className}.php";
        if (file_exists($path)) {
            require_once($path);
        } else {
            die("The file {$classname} could not be found.");
        }
    }

}

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 SP.

JSC0d3's Logo
About JSC0d3

JSC0d3 is an entrepreneur, online marketer, and an employee of an IT company. When not building websites, creating content, or helping customers improve their online business, spend time with their wife and two beautiful children. Although he still feels new in WordPress, he enjoys sharing what he has learned with all of you! If you want to get in touch with him, you can do this through this website.

On the same idea

Posted by | February 9, 2019

WordPress' wp_link_pages() function, used for displaying page links in multi-page posts, lacks one big feature You can display a list of...

Posted by | December 14, 2018

Preliminary step: Testing whether the code complies with the format of a fiscal identification code (CIF) That is, the maximum length should be 10...

Posted by | November 8, 2017

Nowadays, a lot of bloggers choose to remove HTML tag from WordPress websites However, for beginners and newbies in this field, they may feel...

Previous PostBackNext Post

Leave here an impression