@@ -6,58 +6,62 @@ The PSR-4 Class Loader
6
6
7
7
Libraries that follow the `PSR-4 `_ standard can be loaded with the ``Psr4ClassLoader ``.
8
8
9
+ .. versionadded :: 2.5
10
+ The :class: `Symfony\\ Component\\ ClassLoader\\ Psr4ClassLoader ` was
11
+ introduced in Symfony 2.5.
12
+
9
13
.. note ::
10
14
11
15
If you manage your dependencies via Composer, you get a PSR-4 compatible
12
16
autoloader out of the box. Use this loader in environments where Composer
13
17
is not available.
14
18
15
19
.. tip ::
20
+
16
21
All Symfony Components follow PSR-4.
17
22
18
23
Usage
19
24
-----
20
25
21
- The following example demonstrates, how you can use the
26
+ The following example demonstrates how you can use the
22
27
:class: `Symfony\\ Component\\ ClassLoader\\ Psr4ClassLoader ` autoloader to use
23
- Symfony's Yaml component. Let's imagine, you downloaded both components –
24
- ClassLoader and Yaml – as ZIP packages and unpacked them to a libs directory.
25
-
28
+ Symfony's Yaml component. Imagine, you downloaded both the ``ClassLoader `` and
29
+ ``Yaml `` component as ZIP packages and unpacked them to a ``libs `` directory.
26
30
The directory structure will look like this:
27
31
28
32
.. code-block :: text
29
33
30
- /
31
- +- libs
32
- | +- ClassLoader
33
- | | +- Psr4ClassLoader.php
34
- | | +- …
35
- | +- Yaml
36
- | +- Yaml.php
37
- | +- …
38
- +- config.yml
39
- +- test.php
34
+ libs/
35
+ ClassLoader/
36
+ Psr4ClassLoader.php
37
+ ...
38
+ Yaml/
39
+ Yaml.php
40
+ ...
41
+ config.yml
42
+ test.php
40
43
41
- In ``demo.php ``, to parse the file ``config.yml ``, you can use the following
42
- code.
44
+ In ``demo.php `` you are going to parse the ``config.yml `` file. To do that, you
45
+ first need to configure the `` Psr4ClassLoader ``:
43
46
44
47
.. code-block :: php
45
48
46
49
use Symfony\Component\ClassLoader\Psr4ClassLoader;
47
50
use Symfony\Component\Yaml\Yaml;
48
51
49
- require __DIR__ . '/lib/ClassLoader/Psr4ClassLoader.php';
52
+ require __DIR__. '/lib/ClassLoader/Psr4ClassLoader.php';
50
53
51
54
$loader = new Psr4ClassLoader();
52
- $loader->addPrefix('Symfony\\Component\\Yaml\\', __DIR__ . '/lib/Yaml');
55
+ $loader->addPrefix('Symfony\\Component\\Yaml\\', __DIR__. '/lib/Yaml');
53
56
$loader->register();
54
57
55
- $data = Yaml::parse(__DIR__ . '/demo.yml');
58
+ $data = Yaml::parse(__DIR__. '/demo.yml');
56
59
57
- First of all, we've loaded our class loader manually using ``require `` since we
58
- don't have an autoload mechanism, yet. With the ``addPrefix() `` call, we told
59
- the class loader where to look for classes with the namespace prefix
60
- ``Symfony\Component\Yaml\ ``. After registering the autoloader, the Yaml
61
- component is ready to use.
60
+ First of all, the class loader is loaded manually using a ``require ``
61
+ statement, since there is no autoload mechanism yet. With the
62
+ :method: `Symfony\C omponent\C lassLoader\P sr4ClassLoader::addPrefix ` call, you
63
+ tell the class loader where to look for classes with the
64
+ ``Symfony\Component\Yaml\ `` namespace prefix. After registering the autoloader,
65
+ the Yaml component is ready to use.
62
66
63
67
.. _PSR-4 : http://www.php-fig.org/psr/psr-4/
0 commit comments