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

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

Michael Heap

May 20, 2015
Tweet

More Decks by Michael Heap

Other Decks in Technology

Transcript

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

    View Slide

  2. About Me

    View Slide

  3. About Me
    Michael

    View Slide

  4. About Me
    @mheap

    View Slide

  5. About Me
    @DataSift

    View Slide

  6. History

    View Slide

  7. Composer

    View Slide

  8. The FIG

    View Slide

  9. History

    View Slide

  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

    View Slide

  11. Current Members

    View Slide

  12. Accepted

    View Slide

  13. PSR-0
    Autoloading

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  21. Using PSR-0

    View Slide

  22. PSR-0
    Autoloading

    View Slide

  23. PSR-1
    Basic Coding Standard

    View Slide

  24. Use Tabs
    Or Spaces

    View Slide

  25. Requires PSR0

    View Slide

  26. <br/>echo "Hello World";<br/>

    View Slide

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

    View Slide

  28. PSR-1
    Basic Coding Standard

    View Slide

  29. PSR-2
    Coding Style Guide

    View Slide

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

    View Slide

  31. One Statement
    Per Line

    View Slide

  32. No Trailing
    Whitespace

    View Slide

  33. Function Visibility
    Must Be Declared

    View Slide

  34. 4 Spaces
    For Indentation

    View Slide

  35. Property Names
    No Leading
    Underscore

    View Slide

  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

    View Slide

  37. # Breaks PSR2
    if ('awesome' == $tek){
    }
    # Valid. Adds the space
    # before the opening brace
    if ('awesome' == $tek) {
    }

    View Slide

  38. Recommendations
    Not Demands

    View Slide

  39. Using PSR-2

    View Slide

  40. PHPCS
    phpcs --standard=psr2

    View Slide

  41. PHPCBF
    phpcbf --standard=psr2

    View Slide

  42. PSR-2
    Coding Style Guide

    View Slide

  43. PSR-3
    Logger Interface

    View Slide

  44. 18 Yes
    0 No

    View Slide

  45. RFC 5424
    Syslog

    View Slide

  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

    View Slide

  47. $logger->info(
    "This is an informational
    message”
    );

    View Slide

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

    View Slide

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

    View Slide

  50. Using PSR-3

    View Slide

  51. Monolog

    View Slide

  52. Analog

    View Slide

  53. KLogger

    View Slide

  54. PSR-3
    Logger Interface

    View Slide

  55. PSR-4
    Autoloader
    (again)

    View Slide

  56. Replaces PSR-0

    View Slide

  57. No More _

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  61. Using PSR-4

    View Slide

  62. PSR-4
    Autoloader
    (again)

    View Slide

  63. PSR-7
    HTTP Message

    View Slide

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

    View Slide

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

    View Slide

  66. HttpFoundation

    View Slide

  67. Zend\Http

    View Slide

  68. $otherFramework

    View Slide

  69. PSR-7

    View Slide

  70. Requests

    View Slide

  71. Responses

    View Slide

  72. Streaming

    View Slide

  73. File Uploads

    View Slide

  74. HTTP

    View Slide

  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

    View Slide

  76. PSR-7
    HTTP Message

    View Slide

  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

    View Slide

  78. Proposed

    View Slide

  79. PSR-5
    PHPDoc

    View Slide

  80. PSR-6
    Cache

    View Slide

  81. PSR-8
    Huggable Interface

    View Slide

  82. PSR-9
    Security Disclosure

    View Slide

  83. PSR-10
    Security Advisories

    View Slide

  84. @mheap
    joind.in/13759

    View Slide