Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

129 lines (88 sloc) 4.5 kb

The ClassLoader Component

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

Whenever you use an undefined class, PHP uses the autoloading mechanism to delegate the loading of a file defining the class. Symfony2 provides a "universal" autoloader, which is able to load classes from files that implement one of the following conventions:

  • The technical interoperability standards for PHP 5.3 namespaces and class names;
  • The PEAR naming convention for classes.

If your classes and the third-party libraries you use for your project follow these standards, the Symfony2 autoloader is the only autoloader you will ever need.


You can install the component in many different ways:


Registering the :class:`Symfony\\Component\\ClassLoader\\UniversalClassLoader` autoloader is straightforward:

require_once '/path/to/src/Symfony/Component/ClassLoader/UniversalClassLoader.php';

use Symfony\Component\ClassLoader\UniversalClassLoader;

$loader = new UniversalClassLoader();

// You can search the include_path as a last resort.

// ... register namespaces and prefixes here - see below


For minor performance gains class paths can be cached in memory using APC by registering the :class:`Symfony\\Component\\ClassLoader\\ApcUniversalClassLoader`:

require_once '/path/to/src/Symfony/Component/ClassLoader/UniversalClassLoader.php';
require_once '/path/to/src/Symfony/Component/ClassLoader/ApcUniversalClassLoader.php';

use Symfony\Component\ClassLoader\ApcUniversalClassLoader;

$loader = new ApcUniversalClassLoader('apc.prefix.');

The autoloader is useful only if you add some libraries to autoload.


The autoloader is automatically registered in a Symfony2 application (see app/autoload.php).

If the classes to autoload use namespaces, use the :method:`Symfony\\Component\\ClassLoader\\UniversalClassLoader::registerNamespace` or :method:`Symfony\\Component\\ClassLoader\\UniversalClassLoader::registerNamespaces` methods:

$loader->registerNamespace('Symfony', __DIR__.'/vendor/symfony/symfony/src');

    'Symfony' => __DIR__.'/../vendor/symfony/symfony/src',
    'Monolog' => __DIR__.'/../vendor/monolog/monolog/src',


For classes that follow the PEAR naming convention, use the :method:`Symfony\\Component\\ClassLoader\\UniversalClassLoader::registerPrefix` or :method:`Symfony\\Component\\ClassLoader\\UniversalClassLoader::registerPrefixes` methods:

$loader->registerPrefix('Twig_', __DIR__.'/vendor/twig/twig/lib');

    'Swift_' => __DIR__.'/vendor/swiftmailer/swiftmailer/lib/classes',
    'Twig_'  => __DIR__.'/vendor/twig/twig/lib',



Some libraries also require their root path be registered in the PHP include path (set_include_path()).

Classes from a sub-namespace or a sub-hierarchy of PEAR classes can be looked for in a location list to ease the vendoring of a sub-set of classes for large projects:

    'Doctrine\\Common'           => __DIR__.'/vendor/doctrine/common/lib',
    'Doctrine\\DBAL\\Migrations' => __DIR__.'/vendor/doctrine/migrations/lib',
    'Doctrine\\DBAL'             => __DIR__.'/vendor/doctrine/dbal/lib',
    'Doctrine'                   => __DIR__.'/vendor/doctrine/orm/lib',


In this example, if you try to use a class in the Doctrine\Common namespace or one of its children, the autoloader will first look for the class under the doctrine-common directory, and it will then fallback to the default Doctrine directory (the last one configured) if not found, before giving up. The order of the registrations is significant in this case.

Jump to Line
Something went wrong with that request. Please try again.