|
| 1 | +.. index:: |
| 2 | + single: Class Loader; PSR-0 Class Loader |
| 3 | + |
| 4 | +The PSR-0 Class Loader |
| 5 | +====================== |
| 6 | + |
| 7 | +Introduction |
| 8 | +------------ |
| 9 | + |
| 10 | +.. versionadded:: 2.1 |
| 11 | + The ``ClassLoader`` class was added in Symfony 2.1. |
| 12 | + |
| 13 | +If your classes and third-party libraries follow the `PSR-0`_ standards, you |
| 14 | +can use the :class:`Symfony\\Component\\ClassLoader\\ClassLoader` class to |
| 15 | +load all of your project's classes. |
| 16 | + |
| 17 | +.. tip:: |
| 18 | + |
| 19 | + You can use both the ``ApcClassLoader`` and the ``XcacheClassLoader`` to |
| 20 | + :doc:`cache </components/class_loader/cache_class_loader>` a ``ClassLoader`` |
| 21 | + instance or the ``DebugClassLoader`` to :doc:`debug </components/class_loader/debug_class_loader>` |
| 22 | + it. |
| 23 | + |
| 24 | +Usage |
| 25 | +----- |
| 26 | + |
| 27 | +Registering the :class:`Symfony\\Component\\ClassLoader\\ClassLoader` autoloader |
| 28 | +is straightforward:: |
| 29 | + |
| 30 | + require_once '/path/to/src/Symfony/Component/ClassLoader/ClassLoader.php'; |
| 31 | + |
| 32 | + use Symfony\Component\ClassLoader\ClassLoader; |
| 33 | + |
| 34 | + $loader = new ClassLoader(); |
| 35 | + |
| 36 | + // to enable searching the include path (eg. for PEAR packages) |
| 37 | + $loader->useIncludePath(true); |
| 38 | + |
| 39 | + // ... register namespaces and prefixes here - see below |
| 40 | + |
| 41 | + $loader->register(); |
| 42 | + |
| 43 | +.. note:: |
| 44 | + |
| 45 | + The autoloader is automatically registered in a Symfony2 application (see |
| 46 | + ``app/autoload.php``). |
| 47 | + |
| 48 | +Use the :method:`Symfony\\Component\\ClassLoader\\ClassLoader::addPrefix` or |
| 49 | +:method:`Symfony\\Component\\ClassLoader\\ClassLoader::addPrefixes` methods to |
| 50 | +register your classes:: |
| 51 | + |
| 52 | + // register a single namespaces |
| 53 | + $loader->addPrefix('Symfony', __DIR__.'/vendor/symfony/symfony/src'); |
| 54 | + |
| 55 | + // register several namespaces at once |
| 56 | + $loader->addPrefixes(array( |
| 57 | + 'Symfony' => __DIR__.'/../vendor/symfony/symfony/src', |
| 58 | + 'Monolog' => __DIR__.'/../vendor/monolog/monolog/src', |
| 59 | + )); |
| 60 | + |
| 61 | + // register a prefix for a class following the PEAR naming conventions |
| 62 | + $loader->addPrefix('Twig_', __DIR__.'/vendor/twig/twig/lib'); |
| 63 | + |
| 64 | + $loader->addPrefixes(array( |
| 65 | + 'Swift_' => __DIR__.'/vendor/swiftmailer/swiftmailer/lib/classes', |
| 66 | + 'Twig_' => __DIR__.'/vendor/twig/twig/lib', |
| 67 | + )); |
| 68 | + |
| 69 | +Classes from a sub-namespace or a sub-hierarchy of `PEAR`_ classes can be looked |
| 70 | +for in a location list to ease the vendoring of a sub-set of classes for large |
| 71 | +projects:: |
| 72 | + |
| 73 | + $loader->addPrefixes(array( |
| 74 | + 'Doctrine\\Common' => __DIR__.'/vendor/doctrine/common/lib', |
| 75 | + 'Doctrine\\DBAL\\Migrations' => __DIR__.'/vendor/doctrine/migrations/lib', |
| 76 | + 'Doctrine\\DBAL' => __DIR__.'/vendor/doctrine/dbal/lib', |
| 77 | + 'Doctrine' => __DIR__.'/vendor/doctrine/orm/lib', |
| 78 | + )); |
| 79 | + |
| 80 | +In this example, if you try to use a class in the ``Doctrine\Common`` namespace |
| 81 | +or one of its children, the autoloader will first look for the class under the |
| 82 | +``doctrine-common`` directory, and it will then fallback to the default |
| 83 | +``Doctrine`` directory (the last one configured) if not found, before giving up. |
| 84 | +The order of the registrations is significant in this case. |
| 85 | + |
| 86 | +.. _PEAR: http://pear.php.net/manual/en/standards.naming.php |
| 87 | +.. _PSR-0: http://symfony.com/PSR0 |
0 commit comments