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 8185106a8d Merge branch '2.3' into 2.4
* 2.3:
  bumped Symfony version to 2.3.18
  updated VERSION for 2.3.17
  update CONTRIBUTORS for 2.3.17
  updated CHANGELOG for 2.3.17
  added XSD to PHPUnit configuration
  bug #11319 [HttpKernel] Ensure the storage exists before purging it in ProfilerTest
  [Translation] Added unescaping of ids in PoFileLoader
  updated italian translation for validation messages
  [DomCrawler] Fix docblocks and formatting.
  [DomCrawler] Remove the query string and the anchor of the uri of a link
  Simplified the Travis test command
  [Console] Make sure formatter is the same

Conflicts:
	src/Symfony/Component/HttpKernel/Kernel.php
2014-07-08 13:46:35 +02:00
..
Tests Fix class names in ApcUniversalClassLoader tests. 2014-03-29 07:58:16 +01:00
.gitignore Added missing files .gitignore 2013-07-21 14:12:18 +02:00
ApcClassLoader.php fixed types in phpdocs 2014-04-16 12:30:19 +02:00
ApcUniversalClassLoader.php Fixed most of the docblocks/unused namespaces 2012-12-19 08:09:49 +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.4 2014-04-16 10:02:57 +02:00
ClassLoader.php fixed types in phpdocs 2014-04-16 12:30:19 +02:00
ClassMapGenerator.php [ClassLoader] fixed PHP warning on PHP 5.3 2014-06-26 10:33:01 +02:00
composer.json updated version to 2.4 2013-05-16 09:54:39 +02:00
DebugClassLoader.php Merge branch '2.3' into 2.4 2014-04-16 12:34:31 +02: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 made phpdoc types consistent with those defined in Hack 2014-04-15 07:41:45 +02:00
phpunit.xml.dist added XSD to PHPUnit configuration 2014-07-07 11:57:21 +02:00
README.md updated the composer install command to reflect changes in Composer 2013-09-18 09:27:26 +02:00
UniversalClassLoader.php fixed types in phpdocs 2014-04-16 12:30:19 +02:00
WinCacheClassLoader.php fixed types in phpdocs 2014-04-16 12:30:19 +02:00
XcacheClassLoader.php fixed types in phpdocs 2014-04-16 12:30:19 +02: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