Composer & Autoloading¶
Autoloading¶
The function spl_autoload_register() allows to register a function that will be invoked when PHP needs to load a class/interface defined by the user.
In autoload.php:
# custom function
function autoloader($class) {
# __DIR__ -> path of calling file
# $class -> className (hopefully same as file)
# if class is in namespace $class -> Namespace\className (hopefully folders mirror Namespace)
$class = str_replace("\\", DIRECTORY_SEPARATOR, $class); # avoid linux path separator issues
$fileName = sprintf("%s\\path\\%s.php", __DIR__, $class);
# or
$filename = sprintf("%s\\%s.php", __DIR__, $class); # if class is in namespace
if (file_exists($fileName)) {
include $fileName;
}
}
spl_autoload_register('autoloader'); // register function
In file.php:
Note: will fuck up if namespaces exists.
Multiple Autoloading¶
It's possible to resister multiple autoloading functions by calling spl_autoload_register() multiple times.
# prepend adds the function at the start of the queue
# throws selects if errors in loading throw exceptions
spl_autoload_register(callable $func, $throw=TRUE, $prepend=FALSE);
spl_autoload_functions() # return a list of registered functions.
Composer¶
Open Source project for dependency management and autoloading of PHP libraries and classes.
Composer uses composer.json to define dependencies with third-party libraries.
Libraries are downloaded through Packagist and GitHub.
In composer.json:
Installing Dependencies¶
In the same folder of composer.json execute composer install.
Composer will create:
vendor: folder containing all requested librariesvendor\autoload.php: file for class autoloadingcomposer.lock
In alternative composer require <lib> will add the library to the project and create a composer.json if missing.
Note: to ignore the php version use
composer <command> --ignore-platform-reqs
Updating Dependencies¶
To update dependencies use composer update. To update only the autoloading section use composer dump-autoload.
Autoloading Project Classes¶
Composer can also autoload classes belonging to the current project. It's sufficient to add the autoload keyword in the JSON and specify the path and autoload mode.