Up to this point I've used procedural standalone autoloader functions and registered them with spl_autoload_register() to automatically load my (usually) namespaced classes. Lately, though, I've noticed people mentioning the use of autoloader classes in conjunction with some of the prominent PHP frameworks.
Almost all of my code is object oriented these days, but I don't really see advantages to using a class "Autoloader" over a basic function in this instance. And in terms of testability, I feel pretty good about using class_exists() checks in my tests to verify that the procedural functions load files correctly.
So my questions are three:
UPDATE
Below is some example code for a typical autoload function I might employ. It's metacode, so don't look for typos. I organize my directory structures so that they mirror the namespaces. The hypothetical explode_namespaces()
function could theoretically be included as a static method alongside a static autoload()
method in a class, so that's one benefit. It might be cleaner to combine these disparate "utility" functions as methods in a single class.
function autoload($class_name)
{
$root = APP_LIBS; // a directory path constant set at config time
if ($namespaces = explode_namespaces($class_name)) {
$domain = array_shift($namespaces);
$root .= "/$domain/";
$class_name = array_pop($namespaces);
$directories = array();
foreach ($namespaces as $directory) {
$directories[] = $directory;
}
$root .= implode($directories, '/');
}
$file = "$root/$class_name.php";
if (file_exists($file)) {
include $file;
}
}
The spl_autoload_register() function registers any number of autoloaders, enabling for classes and interfaces to be automatically loaded if they are currently not defined. By registering autoloaders, PHP is given a last chance to load the class or interface before it fails with an error.
The PHP Autoloader searches recursively in defined directories for class, trait and interface definitions. Without any further configuration the directory in which the requiring file resides will be used as default class path. File names don't need to obey any convention. All files are searched for class definitions.
Basically it says: "inside this directory, all namespaces are represented by sub directories and classes are <ClassName>. php files." Autoloading is PHP's way to automatically find classes and their corresponding files without having to require all of them manually.
PHP 5 introduced the magic function __autoload() which is automatically called when your code references a class or interface that hasn't been loaded yet. This provides the runtime one last chance to load the definition before PHP fails with an error.
You are comparing functions with methods. That's just syntactic sugar.
Unless you have a map-based autoloader or one that has a builtin dependency table you don't need any class-level attributes to keep track of things (or could resort to static or global vars otherwise). Runtime reconfigurability is not a real necessity in practice either.
You can make a procedural autoloader project-configurable with constants etc. Having constructor properties isn't that significant of a benefit for reuse with a method implementation. It only may look slightly nicer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With