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-09-06 20:45:30 +02:00
..
Tests load test 2012-09-01 03:02:36 -04:00
.gitignore [Components] Tests/Autoloading fixes 2012-05-01 17:51:41 +02:00
ApcClassLoader.php [ClassLoader] fixed typo 2012-07-03 21:28:10 +02:00
ApcUniversalClassLoader.php [2.0][Component][ClassLoader] cs 2012-04-23 07:37:21 +02:00
CHANGELOG.md [ClassLoader] added CHANGELOG 2012-04-26 22:18:09 +02:00
ClassCollectionLoader.php [ClassLoader] fixed order of interfaces in generated class collection caches (closes #4841) 2012-07-10 20:28:16 +02:00
ClassLoader.php fixed phpdoc (closes symfony/ClassLoader#3) 2012-06-19 15:46:22 +02:00
ClassMapGenerator.php fixed CS 2012-07-09 14:54:20 +02:00
composer.json udpated composer.json to 2.2 2012-09-06 20:45:30 +02:00
DebugClassLoader.php fixed CS 2012-07-09 14:54:20 +02:00
DebugUniversalClassLoader.php fixed CS 2012-07-09 14:54:20 +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 [Components] Tests/Autoloading fixes 2012-05-01 17:51:41 +02:00
README.md [Components] Tests/Autoloading fixes 2012-05-01 17:51:41 +02:00
UniversalClassLoader.php merged 2.0 2012-04-25 12:18:06 +02:00
XcacheClassLoader.php [2.1][Component][ClassLoader] cs 2012-04-23 08:41:33 +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

If you also want to run the unit tests that depend on other Symfony Components, install dev dependencies before running PHPUnit:

php composer.phar install --dev