Skip to content

Commit b98f2e6

Browse files
committed
add an introduction and documentation for the ClassLoader class
1 parent 0c17e5b commit b98f2e6

File tree

4 files changed

+66
-78
lines changed

4 files changed

+66
-78
lines changed
Lines changed: 32 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,84 @@
11
.. index::
2-
pair: Autoloader; Configuration
3-
single: Components; ClassLoader
2+
single: Class Loader; MapClassLoader
43

5-
The ClassLoader Component
6-
=========================
4+
The PSR-0 Class Loader
5+
======================
76

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
268
------------
279

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::
2915

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.
3220

3321
Usage
3422
-----
3523

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::
4126

42-
require_once '/path/to/src/Symfony/Component/ClassLoader/UniversalClassLoader.php';
27+
require_once '/path/to/src/Symfony/Component/ClassLoader/ClassLoader.php';
4328

44-
use Symfony\Component\ClassLoader\UniversalClassLoader;
29+
use Symfony\Component\ClassLoader\ClassLoader;
4530

46-
$loader = new UniversalClassLoader();
31+
$loader = new ClassLoader();
4732

48-
// You can search the include_path as a last resort.
33+
// to enable searching the include path (eg. for PEAR packages)
4934
$loader->useIncludePath(true);
5035

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

5338
$loader->register();
5439

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-
6840
.. note::
6941

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

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::
7848

79-
$loader->registerNamespace('Symfony', __DIR__.'/vendor/symfony/symfony/src');
49+
// register a single namespaces
50+
$loader->addPrefix('Symfony', __DIR__.'/vendor/symfony/symfony/src');
8051

81-
$loader->registerNamespaces(array(
52+
// register several namespaces at once
53+
$loader->addPrefixes(array(
8254
'Symfony' => __DIR__.'/../vendor/symfony/symfony/src',
8355
'Monolog' => __DIR__.'/../vendor/monolog/monolog/src',
8456
));
8557

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

96-
$loader->registerPrefixes(array(
61+
$loader->addPrefixes(array(
9762
'Swift_' => __DIR__.'/vendor/swiftmailer/swiftmailer/lib/classes',
9863
'Twig_' => __DIR__.'/vendor/twig/twig/lib',
9964
));
10065

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-
10866
Classes from a sub-namespace or a sub-hierarchy of PEAR classes can be looked
10967
for in a location list to ease the vendoring of a sub-set of classes for large
11068
projects::
11169

112-
$loader->registerNamespaces(array(
70+
$loader->addPrefixes(array(
11371
'Doctrine\\Common' => __DIR__.'/vendor/doctrine/common/lib',
11472
'Doctrine\\DBAL\\Migrations' => __DIR__.'/vendor/doctrine/migrations/lib',
11573
'Doctrine\\DBAL' => __DIR__.'/vendor/doctrine/dbal/lib',
11674
'Doctrine' => __DIR__.'/vendor/doctrine/orm/lib',
11775
));
11876

119-
$loader->register();
120-
12177
In this example, if you try to use a class in the ``Doctrine\Common`` namespace
12278
or one of its children, the autoloader will first look for the class under the
12379
``doctrine-common`` directory, and it will then fallback to the default
12480
``Doctrine`` directory (the last one configured) if not found, before giving up.
12581
The order of the registrations is significant in this case.
12682

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

components/class_loader/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Class Loader
44
.. toctree::
55
:maxdepth: 2
66

7+
introduction
78
class_loader
89
map_class_loader
910
cache_class_loader
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
The Class Loader Component
2+
==========================
3+
4+
The Class Loader Component loads your project classes automatically.
5+
6+
Whenever you use an undefined class, PHP uses the autoloading mechanism to
7+
delegate the loading of a file defining the class. Symfony2 provides two
8+
autoloaders, which are able to load your classes:
9+
10+
* :doc:`A PSR-0 class loader<class_loader>`
11+
12+
* :doc:`Load classes based on class-to-file mapping<map_class_loader>`
13+
14+
Additionally, the Symfony Class Loader Component ships with a set of wrapper
15+
classes which can be used to add additional functionality on top of existing
16+
autoloaders:
17+
18+
* :doc:`cache_class_loader`
19+
20+
* :doc:`debug_class_loader`
21+
22+
Installation
23+
------------
24+
25+
You can install the component in 2 different ways:
26+
27+
* Use the official Git repository (https://github.com/symfony/ClassLoader);
28+
* :doc:`Install it via Composer </components/using_components>` (``symfony/class-loader``
29+
on `Packagist`_).
30+
31+
.. _Packagist: https://packagist.org/packages/symfony/class-loader

components/map.rst.inc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
* :doc:`/components/using_components`
22

3-
* **Class Loader**
3+
* :doc:`/components/class_loader/index`
44

5+
* :doc:`/components/class_loader/introduction`
56
* :doc:`/components/class_loader/class_loader`
67
* :doc:`/components/class_loader/map_class_loader`
78
* :doc:`/components/class_loader/cache_class_loader`

0 commit comments

Comments
 (0)