php-autoload

Created
Was maintained by jlisher
A is a simple system-wide PSR-4 autoloader for PHP classes.
git clone

php-autoload

A is a simple PSR-4 autoloader for PHP classes.

As the majority of PHP libraries have moved to the PSR-4 autoloading, this loader does not provide support for anything else. However, you may add any non PSR-4 file loaders using the autoload_files_dir.

The name php-autoload is derived from the intended location of the autoload.php file, which is /usr/share/php/autoload.php. As /usr/share/ is the system's shared data directory, we trim that, then replace / for -, and finally trim the file extension .php. This leaves us with php-autoload.

Definitions

  • php_root: The root directory used for class lookups. (default: /usr/share/php)
  • autoload_files_dir: The directory containing adhoc PHP files to be loaded. (default: php_root/autoload.d)

About

The autoloader currently consists of 2 main components:

  • The Autoloader PHP class.
  • A file loader for all PHP files under autoload_files_dir.

Autoloader

Autoloader offers a public API for adding extra files to be required.

Autoloader::addPreFile(string $path): void, Autoloader::addPreFiles(array $paths): void: - Used to add files that should be required before registering the autoload callback. - Is no longer available once Autoloader::register() is called.

Autoloader::addFile(string $path): void, Autoloader::addFiles(array $paths): void: - Used to add files that should be required after registering the autoload callback. - Is no longer available once the autoload callback is registered.

Autoloader::register(): void: - This method can only be used once. - Performs the following actions: 1. Requires the files added using the addPreFile methods. 2. Registers the autoload callback. 3. Requires the files added using the addFile methods.

File Loader

The file loader is responsible for loading all adhoc PHP files located within the autoload_files_dir directory. This is a good place to put any helper files that should be loaded before the autoloader has been registered. Its behaviour is very similar to Composer's autoload.files property in composer.json. The order in which the files are loaded cannot be guaranteed. The files should not return anything, only define functionality that may be used later.

Symlinks may be used if the file's location is critical to the functionality provided.

Usage

To use the autoloader you simply need to require the autoload.php file. autoload.php will handle the loading of all necessary files.

Example:

<?php
require_once '/usr/share/php/autoload.php';
// ...

Installing Libraries

As PSR-4 compatibility is the main focus, it is easy to install a library. Simply use php_root as the location of the root namespace and install the libraries in the respective directories for their namespace. If the library does not provide a namespace, but only a file to autoload, the file should be installed in autoload_files_dir.

Examples:

  • guzzlehttp/guzzle:
    • namespace: GuzzleHttp
    • files: functions_include.php
      • Note: these functions are all deprecated at present.
    • install directory: php_root/GuzzleHttp/
    • install symlink:
      • name: autoload_files_dir/guzzlehttp-guzzle.php
      • target: php_root/GuzzleHttp/functions_include.php
  • guzzlehttp/promises:
    • namespace: GuzzleHttp\Promise
    • files: functions_include.php
      • Note: these functions are all deprecated at present.
    • install directory: php_root/GuzzleHttp/Promise/
    • install symlink:
      • name: autoload_files_dir/guzzlehttp-promises.php
      • target: php_root/GuzzleHttp/Promise/functions_include.php
  • ralouphie/getallheaders:
    • namespace: none
    • files: getallheaders.php
    • install file: autoload_files_dir/ralouphie-getallheaders.php