by Devin Yang
(This article was automatically translated.)

Published - 6 years ago ( Updated - 6 years ago )

The full name of PSR is called "PHP Standards Recommendations" (PHP Standards Recommendations). A proposed specification by FIG (PHP Framework Interoperability Group),
This group was initiated by some framework developers in php|tek in 2009, from the first 5 to more than 20 now.

There are many numbers in the "PHP Standard Recommendations", each number represents a different specification .

PHP standard recommendation number 1 ( PSR-1 ), the basic coding standard . For example, the name of the class (Class) must be declared as StudlyCaps, the name of the method (Method) must be declared as camelCase.. etc.
The PHP standard recommendation number 2 ( PSR-2 ) is an extension of PSR-1, which regulates the PHP coding style , such as extends or implements must be on the same line as the class name...etc.
PHP standard recommendation number 4 ( PSR-4 ) defines automatic loading ( Autoloader standard ), Composer is also a member of FIG ,
For example, in the current Laravel framework, his composer.json can see the trace of using PSR-4 automatic loading,
Because the automatic loading of PSR-0 has been listed as deprecated (DEPRECATED), but there is still support in composer , if you really want to use it.

In the table below, I list the approved specifications, and of course there are also those in the review and draft stages. If you are interested, please refer to the link on the right: Recommendations (PSRs)
Num Title Editor Coordinator Sponsors
1 Basic Coding Standard Paul M. Jones N/A N/A
2 Coding Style Guide Paul M. Jones N/A N/A
3 Logger Interface Jordi Boggiano N/A N/A
4 Autoloading Standard Paul M. Jones Phil Sturgeon Larry Garfield
6 Caching Interface Larry Garfield Paul Dragoonis Robert Hafner
7 HTTP Message Interface Matthew Weier O'Phinney Beau Simensen Paul M. Jones
11 Container Interface Matthieu Napoli, David Negrir Matthew Weier O'Phinney Korvin Szanto
13 Hypermedia Links Larry Garfield Matthew Weier O'Phinney Marc Alexander
16 Simple Cache Paul Dragoonis Jordi Boggiano Fabien Potencier
Take PSR-7 HTTP Message Interface as an example:
The reqeust and response objects in the Slim framework support the PSR-7 interface, which makes Slim very free to use any PSR-7 implementation.
The route of the Slim program can not return the \Slim\Http\Response instance. It can return the \GuzzleHttp\Psr7\CachingStream instance,
Although Slim provides a PSR-7 implementation, we are free to change the default PSR 7 objects in Slim to other third-party implementations.
In the Laravel framework, we can obtain a PSR-7 instance reqeust instead of Laravel's request by installing a library.
composer require symfony/psr-http-message-bridge
composer require zendframework/zend-diactoros

Once we installed these kits, we were able to get the PSR-7 requirements.
use Psr\Http\Message\ServerRequestInterface;
Route::get('/', function (ServerRequestInterface $request) {


Tags: php

Devin Yang

Feel free to ask me, if you don't get it.:)

No Comment

Post your comment

Login is required to leave comments

Similar Stories


A brief introduction to the phpenv container environment I built

I don't have time to shoot an introduction video, so I'll just grab some pictures and introduce the container environment deviny/phpenv I use. can be regarded as an evolutionary version of my previous D-Laravel open source project, conceptually extending the use of many Dlaravel operation methods. The update of the container tends to be controlled by the user to build his own image, so I am not very good at changing the version number. In fact, the php version number of D-Laravel seems to have not been changed for a long time:p

php, CentOS, phpize

Can't find phpize on older versions of CentOS, can't compile php extensions.

Can't find phpize on older versions of CentOS, can't compile php extensions. How to install phpize?


MacOS's php is not powerful, use docker to execute laravel installer

It's very simple, just add an "alias" alias laravel='docker run --rm -v ${PWD}:/var/www/html -v ~/.dlaravel/cache:/home/dlaravel/.composer /cache -ti deviny/fpm:8.1.10 /home/dlaravel/.composer/vendor/bin/laravel'