You are browsing the documentation for Symfony 2.6 which is not maintained anymore.
Consider upgrading your projects to Symfony 5.2.
The ClassLoader Component
The ClassLoader Component¶
The ClassLoader component provides tools to autoload your classes and cache their locations for performance.
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¶
You can install the component in 2 different ways:
- Install it via Composer (
symfony/class-loader
on Packagist); - Use the official Git repository (https://github.com/symfony/ClassLoader).
Then, require the vendor/autoload.php
file to enable the autoloading mechanism
provided by Composer. Otherwise, your application won’t be able to find the classes
of this Symfony component.
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.