8000 feature #3686 Documentation of the new PSR-4 class loader. (derrabus) · symfony/symfony-docs@3731e2e · GitHub
[go: up one dir, main page]

Skip to content

Commit 3731e2e

Browse files
committed
feature #3686 Documentation of the new PSR-4 class loader. (derrabus)
This PR was merged into the master branch. Discussion ---------- Documentation of the new PSR-4 class loader. | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes (symfony/symfony#10100) | Applies to | 2.5+ | Fixed tickets | #3655 Hello docs team, this is a first draft of a documentation for the PSR-4 class loaded I've contributed. As this is my first contribution to the documentation, any assistance would be appreciated. :-) Commits ------- cb2be4a Moved versionadded block to the top. 16fead4 Adjustments from comments by @bicpi a05da41 Minor corrections. 6f2a1a3 Adjustments from comments be @wouterj 17166bd First shot of a documentation of the new PSR-4 class loader.
2 parents cd6d1de + cb2be4a commit 3731e2e

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

components/class_loader/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ ClassLoader
66

77
introduction
88
class_loader
9+
psr4_class_loader
910
map_class_loader
1011
cache_class_loader
1112
debug_class_loader
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
.. index::
2+
single: ClassLoader; PSR-4 Class Loader
3+
4+
The PSR-4 Class Loader
5+
======================
6+
7+
.. versionadded:: 2.5
8+
The :class:`Symfony\\Component\\ClassLoader\\Psr4ClassLoader` was
9+
introduced in Symfony 2.5.
10+
11+
Libraries that follow the `PSR-4`_ standard can be loaded with the ``Psr4ClassLoader``.
12+
13+
.. note::
14+
15+
If you manage your dependencies via Composer, you get a PSR-4 compatible
16+
autoloader out of the box. Use this loader in environments where Composer
17+
is not available.
18+
19+
.. tip::
20+
21+
All Symfony components follow PSR-4.
22+
23+
Usage
24+
-----
25+
26+
The following example demonstrates how you can use the
27+
:class:`Symfony\\Component\\ClassLoader\\Psr4ClassLoader` autoloader to use
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.
30+
The directory structure will look like this:
31+
32+
.. code-block:: text
33+
34+
libs/
35+
ClassLoader/
36+
Psr4ClassLoader.php
37+
...
38+
Yaml/
39+
Yaml.php
40+
...
41+
config.yml
42+
demo.php
43+
44+
In ``demo.php`` you are going to parse the ``config.yml`` file. To do that, you
45+
first need to configure the ``Psr4ClassLoader``:
46+
47+
.. code-block:: php
48+
49+
use Symfony\Component\ClassLoader\Psr4ClassLoader;
50+
use Symfony\Component\Yaml\Yaml;
51+
52+
require __DIR__.'/lib/ClassLoader/Psr4ClassLoader.php';
53+
54+
$loader = new Psr4ClassLoader();
55+
$loader->addPrefix('Symfony\\Component\\Yaml\\', __DIR__.'/lib/Yaml');
56+
$loader->register();
57+
58+
$data = Yaml::parse(__DIR__.'/config.yml');
59+
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\Component\ClassLoader\Psr4ClassLoader::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 be used.
66+
67+
.. _PSR-4: http://www.php-fig.org/psr/psr-4/

components/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* :doc:`/components/class_loader/introduction`
66
* :doc:`/components/class_loader/class_loader`
7+
* :doc:`/components/class_loader/psr4_class_loader`
78
* :doc:`/components/class_loader/map_class_loader`
89
* :doc:`/components/class_loader/cache_class_loader`
910
* :doc:`/components/class_loader/debug_class_loader`

0 commit comments

Comments
 (0)
0