Skip to content
[READ-ONLY] Subtree split of the Symfony ClassLoader Component
Latest commit dd93569 @nicolas-grekas nicolas-grekas Merge branch '3.0'
* 3.0: (105 commits)
  [Console] remove readline support
  bumped Symfony version to 3.0.3
  updated VERSION for 3.0.2
  updated CHANGELOG for 3.0.2
  [Routing] added a suggestion to add the HttpFoundation component.
  [FrameworkBundle] fix assets and templating tests
  [ClassLoader] fix ApcClassLoader tests on HHVM
  [travis] Add some comments
  changed operator from and to &&
  [DependencyInjection] Remove unused parameter
  [Process] Fix transient tests for incremental outputs
  [Console] Add missing `@require` annotation in test
  Fix merge
  [appveyor] Fix failure reporting
  [#17634] move DebugBundle license file
  Limit Ldap component version for the 3.0 branch
  backport GlobTest from 2.7 branch
  Move licenses according to new best practices
  [FrameworkBundle] Remove unused code in test
  [2.3] Fixed an undefined variable in Glob::toRegex


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();

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:

    '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);

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.


You can run the unit tests with the following command:

$ cd path/to/Symfony/Component/ClassLoader/
$ composer install
$ phpunit
Something went wrong with that request. Please try again.