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
2012-04-19 13:35:17 +02:00
..
Tests [Tests] Use proper assertions 2012-04-14 10:05:05 +02:00
ApcClassLoader.php fixed CS 2012-04-07 09:10:50 +02:00
ApcUniversalClassLoader.php [ClassLoader] Update PSR-0 reference. 2012-01-11 09:27:00 +05:45
ClassCollectionLoader.php Fix chmod() calls to apply umask 2012-04-19 13:35:17 +02:00
ClassLoader.php fixed CS 2012-04-07 09:10:50 +02:00
ClassMapGenerator.php [ClassLoader] Fixed version compare 2012-03-09 08:17:46 +01:00
composer.json Removed version field 2012-02-27 09:59:20 +01:00
DebugClassLoader.php [ClassLoader] Added a DebugClassLoader using composition 2012-04-02 19:03:58 +02:00
DebugUniversalClassLoader.php merged 2.0 2011-10-04 09:32:13 +02:00
LICENSE Updated LICENSE files copyright 2012-02-22 10:10:37 +01:00
MapClassLoader.php [ClassLoader] replaced MapFileClassLoader by MapClassLoader 2011-07-22 14:44:33 +02:00
phpunit.xml.dist [PhpUnit] Fix the path to the boostrap files in the components 2012-03-30 13:49:28 +02:00
README.md moved component and bridge unit tests to the src/ directory 2012-03-29 08:37:22 +02:00
UniversalClassLoader.php [ClassLoader] Add ability to incrementally register fallbacks. 2012-02-22 07:07:03 +05:45
XcacheClassLoader.php Fixed spelling error 2012-04-09 10:21:42 +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:

phpunit -c src/Symfony/Component/ClassLoader/

If you also want to run the unit tests that depend on other Symfony Components, declare the following environment variables before running PHPUnit:

export SYMFONY_FINDER=../path/to/Finder