This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/ClassLoader
Gyula Sallai ba3b321842 [ClassLoader] Added a class map file generator utility
fixed cs

Small refactoring for Finder support

If class name found, return

Find multiple classes and namespaces in the same file

fixed problems with inheritance and non-php files

Renamed ClassMapDumper to ClassMapGenerator

fixed error with splfileinfo
2012-02-04 13:48:25 +01:00
..
ApcUniversalClassLoader.php [ClassLoader] Update PSR-0 reference. 2012-01-11 09:27:00 +05:45
ClassCollectionLoader.php merged 2.0 2011-09-28 16:08:31 +02:00
ClassMapGenerator.php [ClassLoader] Added a class map file generator utility 2012-02-04 13:48:25 +01:00
composer.json Revert "merged 2.0" 2012-01-08 20:43:02 +01:00
DebugUniversalClassLoader.php merged 2.0 2011-10-04 09:32:13 +02:00
LICENSE added LICENSE files for the subtree repositories 2011-02-22 18:58:15 +01:00
MapClassLoader.php [ClassLoader] replaced MapFileClassLoader by MapClassLoader 2011-07-22 14:44:33 +02:00
README.md tweaked the README files 2011-12-18 14:22:28 +01:00
UniversalClassLoader.php merged 2.0 2012-01-11 15:47:52 +01:00

ClassLoader Component

ClassLoader loads your project classes automatically if they follow some standard PHP conventions.

The Universal ClassLoader is able to autoload classes that implement the PSR-0 standard or the PEAR naming convention.

First, register the autoloader:

require_once __DIR__.'/src/Symfony/Component/ClassLoader/UniversalClassLoader.php';

use Symfony\Component\ClassLoader\UniversalClassLoader;

$loader = new UniversalClassLoader();
$loader->register();

Then, register some namespaces with the registerNamespace() method:

$loader->registerNamespace('Symfony', __DIR__.'/src');
$loader->registerNamespace('Monolog', __DIR__.'/vendor/monolog/src');

The registerNamespace() method takes a namespace prefix and a path where to look for the classes as arguments.

You can also register a sub-namespaces:

$loader->registerNamespace('Doctrine\\Common', __DIR__.'/vendor/doctrine-common/lib');

The order of registration is significant and the first registered namespace takes precedence over later registered one.

You can also register more than one path for a given namespace:

$loader->registerNamespace('Symfony', array(__DIR__.'/src', __DIR__.'/symfony/src'));

Alternatively, you can use the registerNamespaces() method to register more than one namespace at once:

$loader->registerNamespaces(array(
    'Symfony'          => array(__DIR__.'/src', __DIR__.'/symfony/src'),
    'Doctrine\\Common' => __DIR__.'/vendor/doctrine-common/lib',
    'Doctrine'         => __DIR__.'/vendor/doctrine/lib',
    'Monolog'          => __DIR__.'/vendor/monolog/src',
));

For better performance, you can use the APC based version of the universal class loader:

require_once __DIR__.'/src/Symfony/Component/ClassLoader/UniversalClassLoader.php';
require_once __DIR__.'/src/Symfony/Component/ClassLoader/ApcUniversalClassLoader.php';

use Symfony\Component\ClassLoader\ApcUniversalClassLoader;

$loader = new ApcUniversalClassLoader('apc.prefix.');

Furthermore, the component provides tools to aggregate classes into a single file, which is especially useful to improve performance on servers that do not provide byte caches.