Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PSRs: what, why and how?

PSRs: what, why and how?

While increasingly important in our ecosystem, the PHP Standards Recommendations remain too little known and somewhat mystical for many of us. In this talk, I propose to understand what is a PSR and then to briefly review all the existing recommendations, whether validated, accepted or draft. We will also come back to the importance and purpose of these standards as well as their development process.


Julien Janvier

May 20, 2017


  1. PSRs: what, why and how? ___ Julien Janvier @jujanvier https://joind.in/talk/ebe72

  2. PHP Standard Recommendation

  3. What is a PSR?

  4. accepted PSR-1: Basic Coding Standard

  5. accepted PSR-2: Coding Style Guide

  6. draft PSR-12: Extended Coding Style Guide

  7. draft PSR-5: PHPDoc Standard /** * @method int MyMagicMethod(string $argument1)

    { * This is the summary for MyMagicMethod. * * @param string $argument1 Description for argument 1. * * @return int * } */ class MyMagicClass { // ... }
  8. Who is behind these standards?

  9. Framework Interoperability Group www.php-fig.org www.github.com/php-fig/fig-standards @phpfig

  10. ...and many more... ...and many more...

  11. How these standards are written?

  12. Member Project Project Representative Core Committee

  13. Working Group = Editor + Sponsor + Contributors

  14. Pre draft >> Draft >> Review >> Accepted >> Dreprecate

  15. Let’s go!

  16. Autoloading

  17. PSR-0: Autoloading Standard Nelmio\Alice\ => src/ class Nelmio\Alice\Loader\Yaml src/Nelmio/Alice/Loader/Yaml.php class

    Nelmio_Alice_Loader_Yaml deprecated
  18. PSR-0: Autoloading Standard Nelmio\Alice\ => src/ class Nelmio\Alice\Loader\Yaml src/Nelmio/Alice/Loader/Yaml.php accepted

    src/Loader/Yaml.php class Nelmio_Alice_Loader_Yaml PSR-4: Autoloader deprecated
  19. What prompted the FIG to write the first standards back

    in 2009?
  20. What prompted the FIG to write the first standards back

    in 2009? Almost impossible to reuse others’ code.
  21. Security issues

  22. draft PSR-9: Security Reporting Process <link rel="php-vuln-reporting" href="http://security.example.org"/> security@example.org

  23. draft PSR-10: Security Disclosure Publication <link rel="php-vuln-disclosures" href="http://disclosures.example.org"/>

  24. Why knowing existing standards is useful?

  25. Why knowing existing standards is useful? Let us take advantage

    of others’ researches.
  26. Day-to-day tools

  27. accepted PSR-3: Logger Interface

  28. accepted PSR-3: Logger Interface namespace Psr\Log; interface LoggerInterface { /**

    * Action must be taken immediately. * Example: Entire website down, database unavailable, etc. * This should trigger the SMS alerts and wake you up. * * @param string $message * @param array $context * * @return void */ public function alert($message, array $context = array());
  29. Why a standard when there is already “one”?

  30. Why a standard when there is already “one”? Our needs

  31. accepted PSR-11: Container Interface namespace Psr\Container; interface ContainerInterface { /**

    * Finds an entry of the container by its identifier and returns it. * * @param string $id Identifier of the entry to look for. * * @throws NotFoundExceptionInterface No entry was found for this id. * @throws ContainerExceptionInterface Error while retrieving the entry. * * @return mixed Entry. */ public function get($id);
  32. accepted PSR-11: Container Interface

  33. Why a standard when there are already so many libraries?

  34. Why a standard when there are already so many libraries?

    Offer us a world of new possibilities.
  35. accepted PSR-6: Caching Interface // $pool is an instance of

    Psr\Cache\CacheItemPoolInterface // $item is an instance of Psr\Cache\CacheItemInterface $item = $pool->getItem('foo'); if (!$item->isHit()) { $item->set('bar'); $item->expiresAfter(3600); $pool->save($item); } $value = $item->get();
  36. accepted PSR-16: Simple Cache // $cache is an instance of

    Psr\SimpleCache\CacheInterface if (null === $cache->get('foo')) { $cache->set('foo', 'bar', 3600); } $value = $cache->get('foo');
  37. draft PSR-14: Event Manager namespace Psr\EventManager; interface EventManagerInterface { public

    function attach($event, $callback, $priority = 0); public function detach($event, $callback); public function clearListeners($event); public function trigger($event, $target = null, $argv = array()); } interface EventInterface { // ... }
  38. Why a standard for “problems” that already have been resolved

    by our framework?
  39. Why a standard for “problems” that already have been resolved

    by our framework? Avoid to couple our business code to a vendor library.
  40. Web

  41. accepted PSR-7: HTTP Message Interface POST /path HTTP/1.1 Host: example.com

    foo=bar&baz=bat HTTP/1.1 200 OK Content-Type: text/plain This is the response body
  42. accepted PSR-7: HTTP Message Interface Request Response Message ServerRequest Stream

    Uri UploadedFile getBody() getUploadedFiles()
  43. PSR-17: HTTP Factories namespace Psr\Http\Message; use Psr\Http\Message\StreamInterface; interface StreamFactoryInterface {

    public function createStream($content = ''); public function createStreamFromFile($filename, $mode = 'r'); public function createStreamFromResource($resource); } review
  44. accepted PSR-13: Hypermedia Links namespace Psr\Link; interface LinkInterface { public

    function getHref(); public function isTemplated(); public function getRels(); public function getAttributes(); }
  45. What standards about so low levels topics can bring?

  46. What standards about so low levels topics can bring? Maybe

    it opens the gates of PHP native implementations.
  47. PSR-15: HTTP Middlewares Working with Slim Middleware, by Timothy Boronczyk

  48. PSR-15: HTTP Middlewares use Psr\Http\ServerMiddleware\DelegateInterface; use Psr\Http\ServerMiddleware\MiddlewareInterface; use Psr\Http\Message\ServerRequestInterface; class

    CacheMiddleware implements MiddlewareInterface { public function process( ServerRequestInterface $request, DelegateInterface $delegate ) { if (null === $response = $this->cache->get(/**a key**/)) { $response = $delegate->process($request); $this->cache->set(/**a key**/, $response); } return $response; } vote
  49. Why those standards?

  50. Why those standards? Promote interoperability.

  51. PSR-8: Huggable Interface draft

  52. Only members can vote...

  53. ...but everybody can participate :)

  54. www.php-fig.org/get-involved/

  55. Thanks :) ___ Julien Janvier @jujanvier https://joind.in/talk/ebe72

  56. Webography • FIG / FIG 3.0 ◦ The PHP-FIG: Past,

    Present & Future - podcast with a few FIG members ◦ Why PHP-FIG matters - by Evert Pot ◦ FIG 3.0 Proposal - by Larry Garfield ◦ The TL;DR of FIG 3.0 - by Michael Cullum ◦ Michael Cullum Talks About PHP FIG, PSR & Symfony - with Michael Cullum ◦ Phil Sturgeon Talks About API Development, PHP-FIG, PHP Books And The Future Of PHP - with Phil Sturgeon ◦ PHP FIG: Breaking down the Boundaries - by Michael Cullum (video here) • PSR-0 ◦ Is PSR-0 Shortsighted, or are you? - by Phil Sturgeon ◦ Composer and PSR-0: Friends, Not Relatives - by Phil Sturgeon
  57. Webography • PSR 6 ◦ An Open Letter To PHP-FIG

    - by Anthony Ferrara ◦ PSR-6 Has Serious Problems - by Andrew Carter ◦ PSR-6 Is About To Pass And Still Has Serious Problems - by Andrew Carter ◦ [VOTE][Accept] PSR-6 Caching Interfaces - by Paul Dragoonis • PSR 9 / PSR 10 ◦ Call for volunteers - by Michael Hess • PSR-11 ◦ [PSR-11+] An overview of interoperable PHP modules - by David Négrier ◦ [PSR-11+] An update on standard service providers - by David Négrier
  58. Webography • PSR-7 / Middlewares ◦ Two things to know

    about PSR-7 - by Rob Allen ◦ Middleware concept - by Slim framework ◦ Writing PSR-7 middleware - by Rob Allen ◦ HTTP, PSR-7 and Middleware - by Rob Allen ◦ PSR-7 And Middleware, The Future Of PHP - by Matthew Weier O'Phinney ◦ All About Middleware - problems about PRS-7 by Anthony Ferrara ◦ All About PSR-7 Middleware - answer about Anthony Ferrara’s post by Woody Gilk ◦ Dependency Inversion and PSR-7 Bodies - by Woody Gilk ◦ PSR-7 Objects Are Not Immutable - by Andrew Carter ◦ Working with Slim Middleware - by Timothy Boronczyk • PSR-15 / Middlewares ◦ Using Anonymous Classes to Write Middleware - by Matthew Weier O'Phinney ◦ Stack middlewares - by Igor Wiedler