PHP and Standards

PHP and Standards

The PHP FIG is a group established in 2010 with the aim of putting together a set of best practices for PHP apps to follow. Five PSRs have been accepted and a few proposals are currently under discussion. These PSRs are designed to make it easier for people to build packages that work with each other by providing interfaces that they can implement without enforcing an implementation. This talk will cover the existing PSRs and the ones that are under consideration, and will then take a brief look at the de facto standards that people use to fulfil the requirements of each PSR

Bbf9decfbfc2ab5b450ec503749ded28?s=128

Michael Heap

May 20, 2015
Tweet

Transcript

  1. PHP & standards @mheap (at) #phptek

  2. About Me

  3. About Me Michael

  4. About Me @mheap

  5. About Me @DataSift

  6. History

  7. Composer

  8. The FIG

  9. History

  10. Agavi - David Zülke Cake - Nate Abele PEAR -

    Brett Bieber, David Coallier, Helgi Þormar Þorbjörnsson, Travis Swicegood, Phing - Travis Swicegood Solar - Paul Jones Symfony - Stefan Koopmanschap Zend Framework - Matthew Weier O'Phinney and the always amiable (i.e., at-large community member), Cal Evans. May 21st, 2009
  11. Current Members

  12. Accepted

  13. PSR-0 Autoloading

  14. <?php function __autoload($class) { include 'classes/' . $class . '.class.php';

    }
  15. <?php spl_autoload_register(function ($class) { include 'classes/' . $class . '.class.php';

    });
  16. <?php spl_autoload_register(function ($class) { include 'ext/' . $class . '.php';

    });
  17. <?php spl_autoload_register(function ($class) { include 'bananas/' . $class . ‘.inc.php';

    });
  18. Tek_Demo_Example => Tek/Demo/Example.php Tek_Demo_Foo => Tek/Demo/Foo.php Tek_Something => Tek/Something.php

  19. Tek_Demo_Example => Tek/Demo/Example.php Tek_Demo_Foo => Tek/Demo/Foo.php Tek_Something => Tek/Something.php Datasift\Filter\Sentiment

    => Datasift/Filter/Sentiment.php Datasift\Service\Auth => Datasift/Service/Auth.php Datasift\User => Datasift/User.php
  20. Tek_Demo_Example => Tek/Demo/Example.php Tek\Demo\Example => Tek/Demo/Example.php Tek\Demo_File\Example => Tek/Demo_File/Example.php Tek\Demo\Example_Foo

    => Tek/Demo/Example/Foo.php Demo => Demo.php
  21. Using PSR-0

  22. PSR-0 Autoloading

  23. PSR-1 Basic Coding Standard

  24. Use Tabs Or Spaces

  25. Requires PSR0

  26. <script language="php"> echo "Hello World"; </script>

  27. class AdminUser { const PERMISSION_LIST = array("add", "delete"); function createAnnouncement()

    { } }
  28. PSR-1 Basic Coding Standard

  29. PSR-2 Coding Style Guide

  30. http://bit.ly/1PWqjOK

  31. One Statement Per Line

  32. No Trailing Whitespace

  33. Function Visibility Must Be Declared

  34. 4 Spaces For Indentation

  35. Property Names No Leading Underscore

  36. There MUST be one space after the control structure keyword

    There MUST NOT be a space after the opening parenthesis There MUST NOT be a space before the closing parenthesis There MUST be one space between the closing parenthesis and the opening brace
  37. # Breaks PSR2 if ('awesome' == $tek){ } # Valid.

    Adds the space # before the opening brace if ('awesome' == $tek) { }
  38. Recommendations Not Demands

  39. Using PSR-2

  40. PHPCS phpcs --standard=psr2

  41. PHPCBF phpcbf --standard=psr2

  42. PSR-2 Coding Style Guide

  43. PSR-3 Logger Interface

  44. 18 Yes 0 No

  45. RFC 5424 Syslog

  46. Emergency – the system is unusable Alert – immediate action

    is required Critical – critical conditions Error – errors that do not require immediate attention but should be monitored Warning – unusual or undesirable occurrences that are not errors Notice – normal but significant events Info – interesting events Debug – detailed information for debugging purposes
  47. $logger->info( "This is an informational message” );

  48. $logger->alert( "This is an alert message” );

  49. $logger->alert( "This is an alert message", array("user" => $user->id) );

  50. Using PSR-3

  51. Monolog

  52. Analog

  53. KLogger

  54. PSR-3 Logger Interface

  55. PSR-4 Autoloader (again)

  56. Replaces PSR-0

  57. No More _

  58. vendor/ vendor_name/ package_name/ src/ Vn/ Pn/ ClassName.php composer.json: "autoload": {

    "psr-0": { "Vn\\Pn\\": "src/" } }
  59. vendor/ vendor_name/ package_name/ src/ ClassName.php composer.json: "autoload": { "psr-4": {

    "Vn\\Pn\\": "src/" } }
  60. vendor/ vendor_name/ package_name/ src/ Vn/ Pn/ ClassName.php composer.json: "autoload": {

    "psr-4": { "Vn\\Pn\\": "src/Vn/Pn/" } }
  61. Using PSR-4

  62. PSR-4 Autoloader (again)

  63. PSR-7 HTTP Message

  64. POST /path HTTP/1.1 Host: example.com foo=bar&baz=bat

  65. HTTP/1.1 200 OK Content-Type: text/plain This is the response body

  66. HttpFoundation

  67. Zend\Http

  68. $otherFramework

  69. PSR-7

  70. Requests

  71. Responses

  72. Streaming

  73. File Uploads

  74. HTTP

  75. Psr\Http\Message\MessageInterface Psr\Http\Message\RequestInterface Psr\Http\Message\ServerRequestInterface Psr\Http\Message\ResponseInterface Psr\Http\Message\StreamInterface Psr\Http\Message\UriInterface Psr\Http\Message\UploadedFileInterface

  76. PSR-7 HTTP Message

  77. PSR-0 PSR-1 PSR-2 PSR-3 PSR-4 PSR-7 Autoloading Basic Coding Coding

    Style Guide Logger Interface Autoloading HTTP Message Interface
  78. Proposed

  79. PSR-5 PHPDoc

  80. PSR-6 Cache

  81. PSR-8 Huggable Interface

  82. PSR-9 Security Disclosure

  83. PSR-10 Security Advisories

  84. @mheap joind.in/13759