XG Proyect

AutoLoader
in package

This class is an auto loader for use with vanilla PHP projects' testing environment. Use it in the bootstrap to register classes without having to use a framework (which you can, and should if it's a better solution for you) and without having to use includes everywhere.

It assumes that the file path in relation to the namespace follows the PSR-0 standard.

IMPORTANT NOTE: When just registering directories, the class has no ability to discern conflicting class names in different namespaces, which means that classes with the same name will override each other! Always use the registerNamespace()-method if possible!

Inspired by Jess Telford's AutoLoader (http://jes.st/).

Tags
see
http://jes.st/2011/phpunit-bootstrap-and-autoloading-classes/
see
http://petermoulding.com/php/psr
see
http://www.php-fig.org/psr/psr-0/
codeCoverageIgnore
category

Toolbox

author

Helge Söderström helge.soderstrom@schibsted.se

Table of Contents

$directoryClassNames  : array<string|int, mixed>
An array keeping class names as key and their path as the value for classes registered with AutoLoader::registerDirectory().
$namespaceClassNames  : array<string|int, mixed>
An array keeping class names as key and their path as the value for classes registered with AutoLoader::registerNamespace().
loadClass()  : void
Includes a found class in the runtime environment. Strips namespaces.
registerClass()  : void
Caches a found class with the class name as key and its path as value for use when loading on the fly. The class is registered with its class name only, no namespace.
registerDirectory()  : void
Store the filename (sans extension) & full path of all ".php" files found.
registerNamespace()  : void
Store the filename (sans extension) & full path to all ".php" files found for a namespace.

Properties

$directoryClassNames

An array keeping class names as key and their path as the value for classes registered with AutoLoader::registerDirectory().

protected static array<string|int, mixed> $directoryClassNames = array()

$namespaceClassNames

An array keeping class names as key and their path as the value for classes registered with AutoLoader::registerNamespace().

protected static array<string|int, mixed> $namespaceClassNames = array()

Methods

loadClass()

Includes a found class in the runtime environment. Strips namespaces.

public static loadClass(string $className) : void
Parameters
$className : string
Return values
void

registerClass()

Caches a found class with the class name as key and its path as value for use when loading on the fly. The class is registered with its class name only, no namespace.

public static registerClass(string $className, string $fileName) : void
Parameters
$className : string
$fileName : string
Return values
void

registerDirectory()

Store the filename (sans extension) & full path of all ".php" files found.

public static registerDirectory(string $dirName) : void

NOTE: This method will not be able to differentiate the same class names in different namespaces and will therefore overwrite class names if multiple of the same name is found. If possible, use registerNamespace instead!

Parameters
$dirName : string
Return values
void

registerNamespace()

Store the filename (sans extension) & full path to all ".php" files found for a namespace.

public static registerNamespace(string $namespace, string $dirName) : void

The parameter should contain the root namespace as the key and the directory as a value.

Parameters
$namespace : string
$dirName : string
Return values
void

Search results