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
Fabien Potencier 23eafccd4a Merge branch '2.5' into 2.6
* 2.5:
  Added information when an error occured during validation of an answer of a question
  [Console] fixes some typos and phpdoc.
  fix phpdoc's alignment
  Minor phpcs fixes
  [ClassLoader] Fix undefined index in ClassCollectionLoader
2014-12-08 09:43:24 +01:00
..
Tests Merge branch '2.3' into 2.5 2014-12-08 09:43:12 +01:00
.gitignore Added missing files .gitignore 2013-07-21 14:12:18 +02:00
ApcClassLoader.php Docblock fixes 2014-11-30 13:33:44 +00:00
ApcUniversalClassLoader.php Remove aligned '=>' and '=' 2014-10-26 08:30:58 +01:00
CHANGELOG.md duplicated the DebugClassLoader in the Debug component 2013-08-28 12:04:02 +02:00
ClassCollectionLoader.php Merge branch '2.3' into 2.5 2014-12-08 09:43:12 +01:00
ClassLoader.php Docblock fixes 2014-11-30 13:33:44 +00:00
ClassMapGenerator.php define constant only if it wasn't defined before 2014-11-29 13:36:20 +01:00
composer.json updated version to 2.6 2014-05-23 16:36:49 +02:00
DebugClassLoader.php Merge branch '2.3' into 2.5 2014-12-02 21:15:53 +01:00
DebugUniversalClassLoader.php made {@inheritdoc} annotations consistent across the board 2014-04-16 09:04:20 +02:00
LICENSE update year on licenses 2014-01-07 08:19:25 -05:00
MapClassLoader.php Docblock fixes 2014-11-30 13:33:44 +00:00
phpunit.xml.dist removed defaults from PHPUnit configuration 2014-07-07 12:13:42 +02:00
Psr4ClassLoader.php CS fixes 2014-12-04 20:26:11 +00:00
README.md Remove aligned '=>' and '=' 2014-10-26 08:30:58 +01:00
UniversalClassLoader.php Docblock fixes 2014-11-30 13:33:44 +00:00
WinCacheClassLoader.php Docblock fixes 2014-11-30 13:33:44 +00:00
XcacheClassLoader.php Docblock fixes 2014-11-30 13:33:44 +00: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.

Resources

You can run the unit tests with the following command:

$ cd path/to/Symfony/Component/ClassLoader/
$ composer.phar install
$ phpunit