WP_CLI

Autoloader::autoload()publicWP-CLI 1.0

The autoload function that gets registered with the SPL Autoloader system.

Method of the class: Autoloader{}

No Hooks.

Return

null. Nothing (null).

Usage

$Autoloader = new Autoloader();
$Autoloader->autoload( $class );
$class(string) (required)
The class that got requested by the spl_autoloader.

Autoloader::autoload() code WP-CLI 2.8.0-alpha

public function autoload( $class ) {

	// Iterate over namespaces to find a match.
	foreach ( $this->namespaces as $namespace ) {

		// Move on if the object does not belong to the current namespace.
		if ( 0 !== strpos( $class, $namespace['root'] ) ) {
			continue;
		}

		// Remove namespace root level to correspond with root filesystem, and
		// replace the namespace separator "\" by the system-dependent directory separator.
		$filename = str_replace(
			[ $namespace['root'], '\\' ],
			[ '', DIRECTORY_SEPARATOR ],
			$class
		);

		// Remove a leading backslash from the class name.
		$filename = $this->remove_leading_backslash( $filename );

		// Change to lower case if requested.
		if ( $namespace['lowercase'] ) {
			$filename = strtolower( $filename );
		}

		// Change underscores into hyphens if requested.
		if ( $namespace['underscores'] ) {
			$filename = str_replace( '_', '-', $filename );
		}

		// Add base_dir, prefix and suffix.
		$filepath = $namespace['base_dir']
			. $namespace['prefix']
			. $filename
			. $namespace['suffix'];

		// Throw an exception if the file does not exist or is not readable.
		if ( is_readable( $filepath ) ) {
			require_once $filepath;
		}
	}
}