Namespacing and Autoloading

suggest change

Technically, autoloading works by executing a callback when a PHP class is required but not found. Such callbacks usually attempt to load these classes.

Generally, autoloading can be understood as the attempt to load PHP files (especially PHP class files, where a PHP source file is dedicated for a specific class) from appropriate paths according to the class’s fully-qualified name (FQN) when a class is needed.

Suppose we have these classes:

Class file for application\controllers\Base:

<?php
namespace application\controllers { class Base {} }

Class file for application\controllers\Control:

<?php
namespace application\controllers { class Control {} }

Class file for application\models\Page:

<?php
namespace application\models { class Page {} }

Under the source folder, these classes should be placed at the paths as their FQNs respectively:

* `controllers`
  * `Base.php`
  * `Control.php`
* `models`
  * `Page.php`

This approach makes it possible to programmatically resolve the class file path according to the FQN, using this function:

function getClassPath(string $sourceFolder, string $className, string $extension = ".php") {
    return $sourceFolder . "/" . str_replace("\\", "/", $className) . $extension; // note that "/" works as a directory separator even on Windows
}

The spl_autoload_register function allows us to load a class when needed using a user-defined function:

const SOURCE_FOLDER = __DIR__ . "/src";
spl_autoload_register(function (string $className) {
    $file = getClassPath(SOURCE_FOLDER, $className);
    if (is_readable($file)) require_once $file;
});

This function can be further extended to use fallback methods of loading:

const SOURCE_FOLDERS = [__DIR__ . "/src", "/root/src"];
spl_autoload_register(function (string $className) {
    foreach(SOURCE_FOLDERS as $folder) {
        $extensions = [
            // do we have src/Foo/Bar.php5_int64?
            ".php" . PHP_MAJOR_VERSION . "_int" . (PHP_INT_SIZE * 8),
            // do we have src/Foo/Bar.php7?
            ".php" . PHP_MAJOR_VERSION,
            // do we have src/Foo/Bar.php_int64?
            ".php" . "_int" . (PHP_INT_SIZE * 8),
            // do we have src/Foo/Bar.phps?
            ".phps",
            // do we have src/Foo/Bar.php?
            ".php"
        ];
        foreach($extensions as $ext) {
            $path = getClassPath($folder, $className, $ext);
            if (is_readable($path)) {
                require_once $path;
                return;
            }
        }
    }
});

PHP does not load a class when the file that mentions it is loaded. The autoloader runs the first time that class is needed, which can be mid-script or during shutdown. Do not replace a source file, especially one inside a phar, while the request is still running. Requiring a .phps file executes it. That extension is conventionally used to display source, not to load classes.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents