|
1 | 1 | .. index::
|
2 |
| - pair: Autoloader; Configuration |
3 |
| - single: Components; ClassLoader |
| 2 | + single: Class Loader; MapClassLoader |
4 | 3 |
|
5 |
| -The ClassLoader Component |
6 |
| -========================= |
| 4 | +The PSR-0 Class Loader |
| 5 | +====================== |
7 | 6 |
|
8 |
| - The ClassLoader Component loads your project classes automatically if they |
9 |
| - follow some standard PHP conventions. |
10 |
| - |
11 |
| -Whenever you use an undefined class, PHP uses the autoloading mechanism to |
12 |
| -delegate the loading of a file defining the class. Symfony2 provides a |
13 |
| -"universal" autoloader, which is able to load classes from files that |
14 |
| -implement one of the following conventions: |
15 |
| - |
16 |
| -* The technical interoperability `standards`_ for PHP 5.3 namespaces and class |
17 |
| - names; |
18 |
| - |
19 |
| -* The `PEAR`_ naming convention for classes. |
20 |
| - |
21 |
| -If your classes and the third-party libraries you use for your project follow |
22 |
| -these standards, the Symfony2 autoloader is the only autoloader you will ever |
23 |
| -need. |
24 |
| - |
25 |
| -Installation |
| 7 | +Introduction |
26 | 8 | ------------
|
27 | 9 |
|
28 |
| -You can install the component in 2 different ways: |
| 10 | +If your classes and third-party libraries follow the `PSR-0`_ standards or the |
| 11 | +`PEAR`_ naming conventions, you can use the :class:`Symfony\\Component\\ClassLoader\\ClassLoader` |
| 12 | +class to load all of your project's classes. |
| 13 | + |
| 14 | +.. note:: |
29 | 15 |
|
30 |
| -* Use the official Git repository (https://github.com/symfony/ClassLoader); |
31 |
| -* :doc:`Install it via Composer </components/using_components>` (``symfony/class-loader`` on `Packagist`_). |
| 16 | + You can use both the ``ApcClassLoader`` and the ``XcacheClassLoader`` to |
| 17 | + :doc:`cache</components/class_loader/cache_class_loader>` a ``ClassLoader`` |
| 18 | + instance or the ``DebugClassLoader`` to :doc:`debug</components/class_loader/debug_class_loader>` |
| 19 | + it. |
32 | 20 |
|
33 | 21 | Usage
|
34 | 22 | -----
|
35 | 23 |
|
36 |
| -.. versionadded:: 2.1 |
37 |
| - The ``useIncludePath`` method was added in Symfony 2.1. |
38 |
| - |
39 |
| -Registering the :class:`Symfony\\Component\\ClassLoader\\UniversalClassLoader` |
40 |
| -autoloader is straightforward:: |
| 24 | +Registering the :class:`Symfony\\Component\\ClassLoader\\ClassLoader` autoloader |
| 25 | +is straightforward:: |
41 | 26 |
|
42 |
| - require_once '/path/to/src/Symfony/Component/ClassLoader/UniversalClassLoader.php'; |
| 27 | + require_once '/path/to/src/Symfony/Component/ClassLoader/ClassLoader.php'; |
43 | 28 |
|
44 |
| - use Symfony\Component\ClassLoader\UniversalClassLoader; |
| 29 | + use Symfony\Component\ClassLoader\ClassLoader; |
45 | 30 |
|
46 |
| - $loader = new UniversalClassLoader(); |
| 31 | + $loader = new ClassLoader(); |
47 | 32 |
|
48 |
| - // You can search the include_path as a last resort. |
| 33 | + // to enable searching the include path (eg. for PEAR packages) |
49 | 34 | $loader->useIncludePath(true);
|
50 | 35 |
|
51 | 36 | // ... register namespaces and prefixes here - see below
|
52 | 37 |
|
53 | 38 | $loader->register();
|
54 | 39 |
|
55 |
| -For minor performance gains class paths can be cached in memory using APC by |
56 |
| -registering the :class:`Symfony\\Component\\ClassLoader\\ApcUniversalClassLoader`:: |
57 |
| - |
58 |
| - require_once '/path/to/src/Symfony/Component/ClassLoader/UniversalClassLoader.php'; |
59 |
| - require_once '/path/to/src/Symfony/Component/ClassLoader/ApcUniversalClassLoader.php'; |
60 |
| - |
61 |
| - use Symfony\Component\ClassLoader\ApcUniversalClassLoader; |
62 |
| - |
63 |
| - $loader = new ApcUniversalClassLoader('apc.prefix.'); |
64 |
| - $loader->register(); |
65 |
| - |
66 |
| -The autoloader is useful only if you add some libraries to autoload. |
67 |
| - |
68 | 40 | .. note::
|
69 | 41 |
|
70 | 42 | The autoloader is automatically registered in a Symfony2 application (see
|
71 | 43 | ``app/autoload.php``).
|
72 | 44 |
|
73 |
| -If the classes to autoload use namespaces, use the |
74 |
| -:method:`Symfony\\Component\\ClassLoader\\UniversalClassLoader::registerNamespace` |
75 |
| -or |
76 |
| -:method:`Symfony\\Component\\ClassLoader\\UniversalClassLoader::registerNamespaces` |
77 |
| -methods:: |
| 45 | +Use the :method:`Symfony\\Component\\ClassLoader\\ClassLoader::addPrefix` or |
| 46 | +:method:`Symfony\\Component\\ClassLoader\\ClassLoader::addPrefixes` methods to |
| 47 | +register your classes:: |
78 | 48 |
|
79 |
| - $loader->registerNamespace('Symfony', __DIR__.'/vendor/symfony/symfony/src'); |
| 49 | + // register a single namespaces |
| 50 | + $loader->addPrefix('Symfony', __DIR__.'/vendor/symfony/symfony/src'); |
80 | 51 |
|
81 |
| - $loader->registerNamespaces(array( |
| 52 | + // register several namespaces at once |
| 53 | + $loader->addPrefixes(array( |
82 | 54 | 'Symfony' => __DIR__.'/../vendor/symfony/symfony/src',
|
83 | 55 | 'Monolog' => __DIR__.'/../vendor/monolog/monolog/src',
|
84 | 56 | ));
|
85 | 57 |
|
86 |
| - $loader->register(); |
87 |
| - |
88 |
| -For classes that follow the PEAR naming convention, use the |
89 |
| -:method:`Symfony\\Component\\ClassLoader\\UniversalClassLoader::registerPrefix` |
90 |
| -or |
91 |
| -:method:`Symfony\\Component\\ClassLoader\\UniversalClassLoader::registerPrefixes` |
92 |
| -methods:: |
93 |
| - |
94 |
| - $loader->registerPrefix('Twig_', __DIR__.'/vendor/twig/twig/lib'); |
| 58 | + // register a prefix for a class following the PEAR naming conventions |
| 59 | + $loader->addPrefix('Twig_', __DIR__.'/vendor/twig/twig/lib'); |
95 | 60 |
|
96 |
| - $loader->registerPrefixes(array( |
| 61 | + $loader->addPrefixes(array( |
97 | 62 | 'Swift_' => __DIR__.'/vendor/swiftmailer/swiftmailer/lib/classes',
|
98 | 63 | 'Twig_' => __DIR__.'/vendor/twig/twig/lib',
|
99 | 64 | ));
|
100 | 65 |
|
101 |
| - $loader->register(); |
102 |
| - |
103 |
| -.. note:: |
104 |
| - |
105 |
| - Some libraries also require their root path be registered in the PHP |
106 |
| - include path (``set_include_path()``). |
107 |
| - |
108 | 66 | Classes from a sub-namespace or a sub-hierarchy of PEAR classes can be looked
|
109 | 67 | for in a location list to ease the vendoring of a sub-set of classes for large
|
110 | 68 | projects::
|
111 | 69 |
|
112 |
| - $loader->registerNamespaces(array( |
| 70 | + $loader->addPrefixes(array( |
113 | 71 | 'Doctrine\\Common' => __DIR__.'/vendor/doctrine/common/lib',
|
114 | 72 | 'Doctrine\\DBAL\\Migrations' => __DIR__.'/vendor/doctrine/migrations/lib',
|
115 | 73 | 'Doctrine\\DBAL' => __DIR__.'/vendor/doctrine/dbal/lib',
|
116 | 74 | 'Doctrine' => __DIR__.'/vendor/doctrine/orm/lib',
|
117 | 75 | ));
|
118 | 76 |
|
119 |
| - $loader->register(); |
120 |
| - |
121 | 77 | In this example, if you try to use a class in the ``Doctrine\Common`` namespace
|
122 | 78 | or one of its children, the autoloader will first look for the class under the
|
123 | 79 | ``doctrine-common`` directory, and it will then fallback to the default
|
124 | 80 | ``Doctrine`` directory (the last one configured) if not found, before giving up.
|
125 | 81 | The order of the registrations is significant in this case.
|
126 | 82 |
|
127 |
| -.. _standards: http://symfony.com/PSR0 |
128 |
| -.. _PEAR: http://pear.php.net/manual/en/standards.php |
129 |
| -.. _Packagist: https://packagist.org/packages/symfony/class-loader |
| 83 | +.. _PEAR: http://pear.php.net/manual/en/standards.naming.php |
| 84 | +.. _PSR-0: http://symfony.com/PSR0 |
0 commit comments