Hi PSR-0, this is PSR-4. I'm coming for you.

This commit is contained in:
Phil Sturgeon
2014-01-31 16:57:28 -05:00
parent db5fa32fe4
commit 822da9c725
3 changed files with 27 additions and 9 deletions

View File

@@ -4,16 +4,30 @@ isChild: true
## Namespaces {#namespaces_title}
As mentioned above, the PHP community has a lot of developers creating lots of code. This means that one library's PHP code may use the same class name as another library. When both libraries are used in the same namespace, they collide and cause trouble.
As mentioned above, the PHP community has a lot of developers creating lots of code. This means that
one library's PHP code may use the same class name as another library. When both libraries are used
in the same namespace, they collide and cause trouble.
_Namespaces_ solve this problem. As described in the PHP reference manual, namespaces may be compared to operating system directories that _namespace_ files; two files with the same name may co-exist in separate directories. Likewise, two PHP classes with the same name may co-exist in separate PHP namespaces. It's as simple as that.
_Namespaces_ solve this problem. As described in the PHP reference manual, namespaces may be compared
to operating system directories that _namespace_ files; two files with the same name may co-exist in
separate directories. Likewise, two PHP classes with the same name may co-exist in separate PHP
namespaces. It's as simple as that.
It is important for you to namespace your code so that it may be used by other developers without fear of colliding with other libraries.
It is important for you to namespace your code so that it may be used by other developers without fear
of colliding with other libraries.
One recommended way to use namespaces is outlined in [PSR-0][psr0], which aims to provide a standard file, class and namespace convention to allow plug-and-play code.
One recommended way to use namespaces is outlined in [PSR-0][psr0], which aims to provide a standard file,
class and namespace convention to allow plug-and-play code.
In December 2013 the PHP-FIG created a new autoloading standard: [PSR-4][psr4], which one day will
probably replace PSR-0. Currently both are still usable, as PSR-4 required PHP 5.3 and many PHP 5.2-only
projects currently implement PSR-0. If you're going to use an autoloader standard for a new application or
package then you almost certainly want to look into PSR-4.
* [Read about Namespaces][namespaces]
* [Read about PSR-0][psr0]
* [Read about PSR-4][psr4]
[namespaces]: http://php.net/manual/en/language.namespaces.php
[psr0]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
[psr4]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md