Autoload::load( string $class_name ): bool

In this article

Autoloader.

Parameters

$class_namestringrequired
Name of the class name to load.

Return

bool Whether a class was loaded or not.

Source

public static function load($class_name) {
	// Check that the class starts with "Requests" (PSR-0) or "WpOrg\Requests" (PSR-4).
	$psr_4_prefix_pos = strpos($class_name, 'WpOrg\\Requests\\');

	if (stripos($class_name, 'Requests') !== 0 && $psr_4_prefix_pos !== 0) {
		return false;
	}

	$class_lower = strtolower($class_name);

	if ($class_lower === 'requests') {
		// Reference to the original PSR-0 Requests class.
		$file = dirname(__DIR__) . '/library/Requests.php';
	} elseif ($psr_4_prefix_pos === 0) {
		// PSR-4 classname.
		$file = __DIR__ . '/' . strtr(substr($class_name, 15), '\\', '/') . '.php';
	}

	if (isset($file) && file_exists($file)) {
		include $file;
		return true;
	}

	/*
	 * Okay, so the class starts with "Requests", but we couldn't find the file.
	 * If this is one of the deprecated/renamed PSR-0 classes being requested,
	 * let's alias it to the new name and throw a deprecation notice.
	 */
	if (isset(self::$deprecated_classes[$class_lower])) {
		/*
		 * Integrators who cannot yet upgrade to the PSR-4 class names can silence deprecations
		 * by defining a `REQUESTS_SILENCE_PSR0_DEPRECATIONS` constant and setting it to `true`.
		 * The constant needs to be defined before the first deprecated class is requested
		 * via this autoloader.
		 */
		if (!defined('REQUESTS_SILENCE_PSR0_DEPRECATIONS') || REQUESTS_SILENCE_PSR0_DEPRECATIONS !== true) {
			// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
			trigger_error(
				'The PSR-0 `Requests_...` class names in the Requests library are deprecated.'
				. ' Switch to the PSR-4 `WpOrg\Requests\...` class names at your earliest convenience.',
				E_USER_DEPRECATED
			);

			// Prevent the deprecation notice from being thrown twice.
			if (!defined('REQUESTS_SILENCE_PSR0_DEPRECATIONS')) {
				define('REQUESTS_SILENCE_PSR0_DEPRECATIONS', true);
			}
		}

		// Create an alias and let the autoloader recursively kick in to load the PSR-4 class.
		return class_alias(self::$deprecated_classes[$class_lower], $class_name, true);
	}

	return false;
}

User Contributed Notes

You must log in before being able to contribute a note or feedback.