The ClassLoader Component
Warning: You are browsing the documentation for Symfony 2.x, which is no longer maintained.
Read the updated version of this page for Symfony 7.2 (the current stable version).
The ClassLoader component provides tools to autoload your classes and cache their locations for performance.
Caution
The ClassLoader component was deprecated in Symfony 3.3 and it will be removed in 4.0. As an alternative, use Composer's class loading mechanism.
Usage
Whenever you reference a class that has not been required or included yet, PHP uses the autoloading mechanism to delegate the loading of a file defining the class. Symfony provides three autoloaders, which are able to load your classes:
- The PSR-0 Class Loader: loads classes that follow the PSR-0 class naming standard;
- The PSR-4 Class Loader: loads classes that follow the PSR-4 class naming standard;
- MapClassLoader: loads classes using a static map from class name to file path.
Additionally, the Symfony ClassLoader component ships with a wrapper class which makes it possible to cache the results of a class loader.
When using the Debug component, you can also use a special DebugClassLoader that eases debugging by throwing more helpful exceptions when a class could not be found by a class loader.
Installation
1
$ composer require symfony/class-loader
Alternatively, you can clone the https://github.com/symfony/class-loader repository.
Note
If you install this component outside of a Symfony application, you must
require the vendor/autoload.php
file in your code to enable the class
autoloading mechanism provided by Composer. Read
this article for more details.