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
Nicolas Grekas fb9838c2d4 Merge branch '2.7' into 2.8
* 2.7:
  Various fixes esp. on Windows
  Fix the validation of form resources to register the default theme
  Fix the retrieval of the value with property path when using a loader
  [appveyor] minor enhancements
  [Process] Disable failing tests on Windows
  [Translation] Fix the string casting in the XliffFileLoader
  Windows and Intl fixes
  Add appveyor.yml for C.I. on Windows
  [VarDumper] fixed HtmlDumper to target specific the head tag
  [travis] merge php: nightly and deps=high test-matrix lines
  consistently use str_replace to unify directory separators
  Support omitting the <target> node in an .xlf file.
  Fix the handling of values for multiple choice types
  moved PHP nightly to PHP 7.0
  [Security] Add missing docblock in PreAuthenticatedToken

Conflicts:
	.travis.yml
2015-08-27 08:53:13 +02:00
..
Tests Merge branch '2.3' into 2.7 2015-08-26 19:56:37 +02:00
.gitignore Added missing files .gitignore 2013-07-21 14:12:18 +02:00
ApcClassLoader.php [ClassLoader] removes deprecated classes from documentation. 2015-01-03 14:04:11 +01:00
ApcUniversalClassLoader.php Silence invasive deprecation warnings, opt-in for warnings 2015-06-08 10:37:21 +01:00
CHANGELOG.md Normalizes deprecation notice messages. 2015-01-05 16:02:28 +01:00
ClassCollectionLoader.php Merge branch '2.3' into 2.5 2014-12-08 09:43:12 +01:00
ClassLoader.php [2.3] CS Fixes 2014-12-21 15:56:12 +01:00
ClassMapGenerator.php Skip ::class constant 2015-07-27 20:12:35 +02:00
composer.json Merge branch '2.7' into 2.8 2015-05-12 17:16:46 +02:00
DebugClassLoader.php Silence invasive deprecation warnings, opt-in for warnings 2015-06-08 10:37:21 +01:00
DebugUniversalClassLoader.php Silence invasive deprecation warnings, opt-in for warnings 2015-06-08 10:37:21 +01:00
LICENSE Updated copyright to 2015 2015-01-01 13:56:52 +01:00
MapClassLoader.php Docblock fixes 2014-11-30 13:33:44 +00:00
phpunit.xml.dist [2.3] require-dev PHPUnit bridge 2015-02-24 11:24:26 +01:00
Psr4ClassLoader.php CS fixes 2014-12-04 20:26:11 +00: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 Silence invasive deprecation warnings, opt-in for warnings 2015-06-08 10:37:21 +01:00
WinCacheClassLoader.php [ClassLoader] removes deprecated classes from documentation. 2015-01-03 14:04:11 +01:00
XcacheClassLoader.php [ClassLoader] removes deprecated classes from documentation. 2015-01-03 14:04:11 +01: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