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

@@ -5,10 +5,10 @@ PHP developers to choose several of these and combine them into a single project
(as close as possible) to a common code style to make it easy for developers to mix and match various libraries for (as close as possible) to a common code style to make it easy for developers to mix and match various libraries for
their projects. their projects.
The [Framework Interop Group][fig] has proposed and approved a series of style recommendations, known as [PSR-0][psr0], The [Framework Interop Group][fig] has proposed and approved a series of style recommendations. Not all of them related
[PSR-1][psr1] and [PSR-2][psr2]. Don't let the funny names confuse you, these recommendations are merely to code-style, but those that do are [PSR-0][psr0], [PSR-1][psr1], [PSR-2][psr2] and [PSR-4][psr4]. These recommendations
a set of rules that some projects like Drupal, Zend, Symfony, CakePHP, phpBB, AWS SDK, FuelPHP, Lithium, etc are starting are merely a set of rules that some projects like Drupal, Zend, Symfony, CakePHP, phpBB, AWS SDK, FuelPHP, Lithium,
to adopt. You can use them for your own projects, or continue to use your own personal style. etc are starting to adopt. You can use them for your own projects, or continue to use your own personal style.
Ideally you should write PHP code that adheres to a known standard. This could be any combination of PSR's, or one Ideally you should write PHP code that adheres to a known standard. This could be any combination of PSR's, or one
of the coding standards made by PEAR or Zend. This means other developers can easily read and work with your code, of the coding standards made by PEAR or Zend. This means other developers can easily read and work with your code,
@@ -17,6 +17,7 @@ and applications that implement the components can have consistency even when wo
* [Read about PSR-0][psr0] * [Read about PSR-0][psr0]
* [Read about PSR-1][psr1] * [Read about PSR-1][psr1]
* [Read about PSR-2][psr2] * [Read about PSR-2][psr2]
* [Read about PSR-4][psr4]
* [Read about PEAR Coding Standards][pear-cs] * [Read about PEAR Coding Standards][pear-cs]
* [Read about Zend Coding Standards][zend-cs] * [Read about Zend Coding Standards][zend-cs]
* [Read about Symfony Coding Standards][symfony-cs] * [Read about Symfony Coding Standards][symfony-cs]
@@ -34,6 +35,7 @@ by all current and future parties who may be working on the codebase.
[psr0]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md [psr0]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
[psr1]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md [psr1]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md
[psr2]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md [psr2]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md
[psr4]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md
[pear-cs]: http://pear.php.net/manual/en/standards.php [pear-cs]: http://pear.php.net/manual/en/standards.php
[zend-cs]: http://framework.zend.com/wiki/display/ZFDEV2/Coding+Standards [zend-cs]: http://framework.zend.com/wiki/display/ZFDEV2/Coding+Standards
[symfony-cs]: http://symfony.com/doc/current/contributing/code/standards.html [symfony-cs]: http://symfony.com/doc/current/contributing/code/standards.html

View File

@@ -4,16 +4,30 @@ isChild: true
## Namespaces {#namespaces_title} ## 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 Namespaces][namespaces]
* [Read about PSR-0][psr0] * [Read about PSR-0][psr0]
* [Read about PSR-4][psr4]
[namespaces]: http://php.net/manual/en/language.namespaces.php [namespaces]: http://php.net/manual/en/language.namespaces.php
[psr0]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md [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

View File

@@ -69,7 +69,7 @@ one database system that another is missing from another by wrapping your querie
This will of course add a little overhead, but if you are building a portable application that needs to work with MySQL, PostgreSQL and This will of course add a little overhead, but if you are building a portable application that needs to work with MySQL, PostgreSQL and
SQLite then a little overhead will be worth it the sake of code cleanliness. SQLite then a little overhead will be worth it the sake of code cleanliness.
Some abstraction layers have been built using the PSR-0 namespace standard so can be installed in any application you like: Some abstraction layers have been built using the [PSR-0][psr0] or [PSR-4][psr4] namespace standards so can be installed in any application you like:
* [Aura SQL][6] * [Aura SQL][6]
* [Doctrine2 DBAL][2] * [Doctrine2 DBAL][2]
@@ -88,3 +88,5 @@ Some abstraction layers have been built using the PSR-0 namespace standard so ca
[mysql]: http://php.net/mysql [mysql]: http://php.net/mysql
[mysqli]: http://php.net/mysqli [mysqli]: http://php.net/mysqli
[pgsql]: http://php.net/pgsql [pgsql]: http://php.net/pgsql
[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