Skip to content

Commit

Permalink
Adjustments from comments be @wouterj
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Mar 16, 2014
1 parent 17166bd commit 6f2a1a3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
52 changes: 28 additions & 24 deletions components/class_loader/psr4_class_loader.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,58 +6,62 @@ The PSR-4 Class Loader

Libraries that follow the `PSR-4`_ standard can be loaded with the ``Psr4ClassLoader``.

.. versionadded:: 2.5
The :class:`Symfony\\Component\\ClassLoader\\Psr4ClassLoader` was
introduced in Symfony 2.5.

.. note::

If you manage your dependencies via Composer, you get a PSR-4 compatible
autoloader out of the box. Use this loader in environments where Composer
is not available.

.. tip::

All Symfony Components follow PSR-4.

Usage
-----

The following example demonstrates, how you can use the
The following example demonstrates how you can use the
:class:`Symfony\\Component\\ClassLoader\\Psr4ClassLoader` autoloader to use
Symfony's Yaml component. Let's imagine, you downloaded both components –
ClassLoader and Yaml – as ZIP packages and unpacked them to a libs directory.

Symfony's Yaml component. Imagine, you downloaded both the ``ClassLoader`` and
``Yaml`` component as ZIP packages and unpacked them to a ``libs`` directory.
The directory structure will look like this:

.. code-block:: text
/
+- libs
| +- ClassLoader
| | +- Psr4ClassLoader.php
| | +- …
| +- Yaml
| +- Yaml.php
| +- …
+- config.yml
+- test.php
libs/
ClassLoader/
Psr4ClassLoader.php
...
Yaml/
Yaml.php
...
config.yml
test.php
In ``demo.php``, to parse the file ``config.yml``, you can use the following
code.
In ``demo.php`` you are going to parse the ``config.yml`` file. To do that, you
first need to configure the ``Psr4ClassLoader``:

.. code-block:: php
use Symfony\Component\ClassLoader\Psr4ClassLoader;
use Symfony\Component\Yaml\Yaml;
require __DIR__ . '/lib/ClassLoader/Psr4ClassLoader.php';
require __DIR__.'/lib/ClassLoader/Psr4ClassLoader.php';
$loader = new Psr4ClassLoader();
$loader->addPrefix('Symfony\\Component\\Yaml\\', __DIR__ . '/lib/Yaml');
$loader->addPrefix('Symfony\\Component\\Yaml\\', __DIR__.'/lib/Yaml');
$loader->register();
$data = Yaml::parse(__DIR__ . '/demo.yml');
$data = Yaml::parse(__DIR__.'/demo.yml');
First of all, we've loaded our class loader manually using ``require`` since we
don't have an autoload mechanism, yet. With the ``addPrefix()`` call, we told
the class loader where to look for classes with the namespace prefix
``Symfony\Component\Yaml\``. After registering the autoloader, the Yaml
component is ready to use.
First of all, the class loader is loaded manually using a ``require``
statement, since there is no autoload mechanism yet. With the
:method:`Symfony\Component\ClassLoader\Psr4ClassLoader::addPrefix` call, you
tell the class loader where to look for classes with the
``Symfony\Component\Yaml\`` namespace prefix. After registering the autoloader,
the Yaml component is ready to use.

.. _PSR-4: http://www.php-fig.org/psr/psr-4/
1 change: 1 addition & 0 deletions components/map.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* :doc:`/components/class_loader/introduction`
* :doc:`/components/class_loader/class_loader`
* :doc:`/components/class_loader/psr4_class_loader`
* :doc:`/components/class_loader/map_class_loader`
* :doc:`/components/class_loader/cache_class_loader`
* :doc:`/components/class_loader/debug_class_loader`
Expand Down

0 comments on commit 6f2a1a3

Please sign in to comment.