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
2016-01-15 10:01:44 +01:00
..
Tests use nowdoc instead of heredoc 2015-12-21 17:05:00 +01:00
.gitignore Added missing files .gitignore 2013-07-21 14:12:18 +02:00
ApcClassLoader.php remove api tags from code 2015-09-28 19:11:22 +02:00
ApcUniversalClassLoader.php remove api tags from code 2015-09-28 19:11:22 +02:00
CHANGELOG.md [ClassLoader] added missing CHANGELOG entry for previous merge 2013-03-23 09:02:48 +01:00
ClassCollectionLoader.php Add gc_mem_caches() call for PHP7 after itoken_get_all() as new memory manager will not release small buckets to OS automatically 2016-01-15 10:01:44 +01:00
ClassLoader.php CS: general fixes 2015-12-01 23:08:33 +01:00
ClassMapGenerator.php Add gc_mem_caches() call for PHP7 after itoken_get_all() as new memory manager will not release small buckets to OS automatically 2016-01-15 10:01:44 +01:00
composer.json added the new Composer exclude-from-classmap option 2015-10-30 12:48:51 -07:00
DebugClassLoader.php remove api tags from code 2015-09-28 19:11:22 +02:00
DebugUniversalClassLoader.php made {@inheritdoc} annotations consistent across the board 2014-04-16 09:04:20 +02:00
LICENSE Update copyright year 2016-01-01 23:53:47 -03:00
MapClassLoader.php Docblock fixes 2014-11-30 13:33:44 +00:00
phpunit.xml.dist Add missing exclusions from phpunit.xml.dist 2015-11-18 09:19:46 +01:00
README.md renamed composer.phar to composer to be consistent with the Symfony docs 2015-02-08 08:41:14 +01:00
UniversalClassLoader.php remove api tags from code 2015-09-28 19:11:22 +02:00
WinCacheClassLoader.php [ClassLoader] removes deprecated classes from documentation. 2015-01-03 14:04:11 +01:00
XcacheClassLoader.php remove api tags from code 2015-09-28 19:11:22 +02:00

ClassLoader Component

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

The ClassLoader object 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/ClassLoader.php';

use Symfony\Component\ClassLoader\ClassLoader;

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

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

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

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

You can also register a sub-namespaces:

$loader->addPrefix('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->addPrefix('Symfony', array(__DIR__.'/src', __DIR__.'/symfony/src'));

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

$loader->addPrefixes(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 class loader:

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

use Symfony\Component\ClassLoader\ClassLoader;
use Symfony\Component\ClassLoader\ApcClassLoader;

$loader = new ClassLoader();
$loader->addPrefix('Symfony', __DIR__.'/src');

$loader = new ApcClassLoader('apc.prefix.', $loader);
$loader->register();

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 install
$ phpunit