-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Documentation of the new PSR-4 class loader. #3686
Changes from all commits
17166bd
6f2a1a3
a05da41
16fead4
cb2be4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ ClassLoader | |
|
||
introduction | ||
class_loader | ||
psr4_class_loader | ||
map_class_loader | ||
cache_class_loader | ||
debug_class_loader |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
.. index:: | ||
single: ClassLoader; PSR-4 Class Loader | ||
|
||
The PSR-4 Class Loader | ||
====================== | ||
|
||
.. versionadded:: 2.5 | ||
The :class:`Symfony\\Component\\ClassLoader\\Psr4ClassLoader` was | ||
introduced in Symfony 2.5. | ||
|
||
Libraries that follow the `PSR-4`_ standard can be loaded with the ``Psr4ClassLoader``. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should add a .. 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would also be a good thing to comment on how this compares to the composer psr4 autoloader or if they are relatively the same There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so, you never have to choose which autoloader to use, you should always use the composer autoloader when available. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh i see, but still this note would actually address the need to know when using this autoloader how it compares to the one from composer. If say if the composer autoloader is way better than this implementation then it would actually encourage the user to move their projects to composer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure, if this is the right place to address a comparison between composer and the ClassLoader component, as this question would apply to the PSR-0 and class map autoloaders as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. true, but still would be good to know the answer 👶 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, so if you manage your project and all of its dependencies with composer, you won't need the ClassLoader component at all, since composer will generate a configured autoloader that works out of the box. Still, you could use ClassLoader, but why should you? On the other hand, if for some reason, you cannot (or do not want to) use composer, composer's autoloader won't help you. In my case, I'm modernizing a legacy application and just need certain Symfony components there. Switching this legacy application to composer is not an option right now, so I'm using the ClassLoader component. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cordoval The implementations are equivalent (the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Anyway, do you think this note just confuses the reader? The more I think about it, the more I'd like to remove it in favor of a general clarification about composer autoloading in Symfony projects. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the note is ok, leave it, unless @wouterj thinks otherwise |
||
|
||
.. tip:: | ||
|
||
All Symfony components follow PSR-4. | ||
|
||
Usage | ||
----- | ||
|
||
The following example demonstrates how you can use the | ||
:class:`Symfony\\Component\\ClassLoader\\Psr4ClassLoader` autoloader to use | ||
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be part of the previous paragraph |
||
|
||
.. code-block:: text | ||
|
||
libs/ | ||
ClassLoader/ | ||
Psr4ClassLoader.php | ||
... | ||
Yaml/ | ||
Yaml.php | ||
... | ||
config.yml | ||
demo.php | ||
|
||
In ``demo.php`` you are going to parse the ``config.yml`` file. To do that, you | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you talk about demo.php here, you mean test.php? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. |
||
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'; | ||
|
||
$loader = new Psr4ClassLoader(); | ||
$loader->addPrefix('Symfony\\Component\\Yaml\\', __DIR__.'/lib/Yaml'); | ||
$loader->register(); | ||
|
||
$data = Yaml::parse(__DIR__.'/config.yml'); | ||
|
||
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 be used. | ||
|
||
.. _PSR-4: http://www.php-fig.org/psr/psr-4/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd move this above the first paragraph.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, why not. /ping @wouterj
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, that's also what I meant.
Btw, you can trust @xabbuh's comments, he has much experience with the symfony docs & reviews :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do trust him, I just wanted to hear your opinion because you suggested to add this paragraph. :-)