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

Modern PHP, Standards, and Community

Ben Edmunds
September 29, 2016

Modern PHP, Standards, and Community

Learn how to structure and maintain a modern day PHP project using the latest standards. We'll walk through recent language improvements and how they will affect your day to day development. This will use code examples to give you in depth, real world examples of usage.

We'll also cover the latest community initiatives and standards including the PHPFIG along with the PSRs they have introduced.

Ben Edmunds

September 29, 2016
Tweet

More Decks by Ben Edmunds

Other Decks in Technology

Transcript

  1. PHP
    modern
    standards &
    Community

    View Slide

  2. Who is this guy?
    Ben Edmunds
    @benedmunds
    http://benedmunds.com

    View Slide

  3. Who is this guy?
    Ben Edmunds
    Open Source
    Author
    PHP Town Hall Podcast
    CTO at Mindfulware

    View Slide

  4. Welcome to
    the Future

    View Slide

  5. Welcome to the Future
    Exceptions
    Namespaces
    Closures

    View Slide

  6. Welcome to the Future
    PDO
    Short Arrays
    Null Coalescing

    View Slide

  7. Welcome to the Future
    Scalar Type Hints

    Return Type
    Declarations

    View Slide

  8. Legit Tools

    View Slide

  9. Legit Tools
    Built-in Server
    Unit Testing
    Composer

    View Slide

  10. Standards

    View Slide

  11. Standards
    PHP-FIG
    PSRs

    View Slide

  12. Community

    View Slide

  13. Community
    Twitter
    etc
    User Groups

    View Slide

  14. Welcome to
    the Future

    View Slide

  15. Great Scott!

    View Slide

  16. Exceptions

    View Slide

  17. View Slide

  18. Exceptions
    try {

    //your code goes here

    }

    catch (Exception $e) {

    die($e->getMessage());

    }

    View Slide

  19. Exceptions
    try {

    //your code goes here

    }

    catch (Exception $e) {

    die($e->getMessage());

    }

    View Slide

  20. Errors
    Exception implements Throwable
    Error implements Throwable

    View Slide

  21. Errors
    try {

    //error thrown here

    }

    catch (Error $e) {

    die($e->getMessage());

    }

    View Slide

  22. Errors
    try {

    //error thrown here

    } catch (Error $e) {

    die($e->getMessage());

    } catch (Exception $e) {

    die($e->getMessage());

    }

    View Slide

  23. Closures

    View Slide

  24. View Slide

  25. Closures
    Route::get(‘/', function(){



    return View::make(‘index');

    });

    View Slide

  26. Closures
    Route::get(‘/', function(){



    return View::make(‘index');

    });

    View Slide

  27. Namespaces

    View Slide

  28. View Slide

  29. Namespaces
    namespace Illuminate\Console;

    class Command

    {

    //…

    View Slide

  30. Namespaces
    use Illuminate\Console\Command;
    namespace Illuminate\Console;

    class Command

    {

    //…

    View Slide

  31. Namespaces
    use Illuminate\Console\Command;
    namespace Illuminate\Console;

    class Command

    {

    //…

    View Slide

  32. Short Array
    Syntax

    View Slide

  33. View Slide

  34. Short Array Syntax
    $array = array(

    0 => ‘value1’,

    1 => ‘value2’,

    );

    View Slide

  35. Short Array Syntax
    $array = [

    0 => ‘value1’,

    1 => ‘value2’,

    ];

    View Slide

  36. Short Array Syntax
    $array = [

    0 => ‘value1’,

    1 => ‘value2’,

    ];

    View Slide

  37. Null
    Coalescing
    Operator

    View Slide

  38. Null Coalescing
    $val = $_GET['val'] ?? 'default';

    View Slide

  39. Null Coalescing
    $val = $_GET['val'] ?? 'default';

    View Slide

  40. Null Coalescing
    $val = isset($_GET[‘val’]) ?

    $_GET[‘val’] : 'default';

    View Slide

  41. Traits

    View Slide

  42. Traits
    // grouping without

    // strict inheritance

    trait baseUser {

    function getName() {

    return ‘Jon Snow’;

    }

    }

    View Slide

  43. Traits
    class adminUser {

    use baseUser;

    }

    View Slide

  44. Traits
    $adminUser = new adminUser;

    echo $adminUser->getName();

    //output = ‘Jon Snow’

    View Slide

  45. PDO

    View Slide

  46. View Slide

  47. PDO
    Cross System

    View Slide

  48. PDO
    Cross System
    MS SQL

    MySQL

    Oracle

    PostgreSQL

    SQLite
    CUBRID

    Firebird

    Informix

    ODBC & DB2

    4D

    View Slide

  49. PDO
    Cross System
    Safe Binding

    View Slide

  50. PDO
    $stmt = $db->prepare(‘

    SELECT * FROM users

    WHERE id=:id

    ’);

    $stmt->bindParam(‘:id’, $id);

    $stmt->execute();

    View Slide

  51. Types!

    View Slide

  52. View Slide

  53. Scalar Type
    Hinting

    View Slide

  54. Scalar Types
    declare(strict_types=1);

    function addNums(float $a, float $b) {

    return $a + $b;

    }

    View Slide

  55. Scalar Types
    declare(strict_types=1);

    function addNums(float $a, float $b) {

    return $a + $b;

    }

    addNums(2, "1 week");

    // Fatal error: Uncaught TypeError:
    Argument 2 passed to addNums() must
    be of the type float, string given

    View Slide

  56. Scalar Types
    function addNums(float $a, float $b)

    addNums(2, "1 week”);

    // Fatal error: Uncaught TypeError:
    Argument 2 passed to addNums()
    must be of the type float, string given

    View Slide

  57. Scalar Types
    try {

    addNums(2, "1 week”);

    }

    catch(TypeError $e) {}

    View Slide

  58. Scalar Types
    try {

    addNums(2, 1);

    }

    catch(TypeError $e) {}

    //3

    View Slide

  59. Scalar Types
    class/interface

    array

    callable

    bool

    float

    int

    string

    View Slide

  60. Return Type
    Declarations

    View Slide

  61. Scalar Types
    function addNums(float $a, float $b) : float {

    return $a + $b;

    }

    View Slide

  62. Scalar Types
    function addNums(float $a, float $b) : float {

    return $a + $b;

    }

    View Slide

  63. Scalar Types
    function addNums($a, $b) : int {

    return $a + $b;

    }

    addNums(1.5, 1);

    // Fatal error: Uncaught TypeError: Return value of
    addNums() must be of the type integer, float
    returned

    View Slide

  64. Legit Tools

    View Slide

  65. View Slide

  66. Built-in
    Web Server

    View Slide

  67. Built-in Server
    $ php -S localhost:8000

    PHP 5.7.0 Development Server started…
    Listening on localhost:8000

    Document root is /home/ben/htdocs

    Press Ctrl-C to quit

    View Slide

  68. Composer

    View Slide

  69. Another
    Package Manager!?

    View Slide

  70. Composer
    Sane Package

    Management

    View Slide

  71. Composer
    Autoloading

    View Slide

  72. Composer
    PEAR, ha!
    packagist.org

    View Slide

  73. Composer
    / composer.json

    {

    "require": {

    "stripe/stripe-php": "dev-master",

    "twilio/sdk": "dev-master"

    }

    }

    View Slide

  74. Composer
    $ php composer.phar update
    $ php composer.phar install

    View Slide

  75. Composer
    $client =

    new Services_Twilio($sid, $tkn);
    $client->account

    ->messages

    ->sendMessage(…)

    View Slide

  76. Unit Testing

    View Slide

  77. View Slide

  78. Unit Testing
    PHPUnit

    Behat

    Mink
    Selenium

    CodeCeption

    PHPSpec

    View Slide

  79. Unit Testing
    class ApiAuthTest extends PHPUnit_Framework_TestCase {

    public function testVerify() {

    $auth = new apiAuth();



    $this->assertTrue($auth->verify());

    View Slide

  80. Unit Testing
    class ApiAuthTest extends PHPUnit_Framework_TestCase {

    public function testVerify() {

    $auth = new apiAuth();



    $this->assertTrue($auth->verify());

    View Slide

  81. Unit Testing
    $ phpunit tests

    PHPUnit 3.3.17 by Sebastian Bergmann.

    Time: 0.01 seconds

    OK (1 tests, 1 assertions)

    View Slide

  82. Standards

    View Slide

  83. View Slide

  84. Standards
    PHP-FIG
    Framework

    Interop

    Group

    View Slide

  85. Standards
    Member Projects
    Zend

    Symfony

    CakePHP
    Laravel

    SugarCRM

    Drupal

    View Slide

  86. Standards
    PSRs
    PHP

    Standards

    Recommendation

    View Slide

  87. Standards
    PSRs
    Autoloading

    Interfaces

    Style Guides

    View Slide

  88. Standards
    PSRs
    PSR-4: Autoloading

    PSR-1: Basic Coding Standards

    PSR-2: Coding Style Guide

    PSR-7: HTTP Message Interface

    PSR-6: Caching Interface

    PSR-3: Logger Interface

    View Slide

  89. Standards
    PSR-4
    Auto-loading
    Replaces PSR-0
    Used by Composer

    View Slide

  90. Standards
    $client =

    new Services_Twilio($sid, $tkn);
    $client->account

    ->messages

    ->sendMessage(…)

    View Slide

  91. Standards
    PSR-1
    Basic Coding Styles
    Generally accepted
    Not very opinionated

    View Slide

  92. Standards
    PSR-1
    Basic Coding Styles
    camelCasing
    Opening Tags
    Namespacing
    Class Names
    UTF-8

    View Slide

  93. Standards
    PSR-2
    Coding Style Guide
    Greater Detail
    Opinionated

    View Slide

  94. Standards
    PSR-2
    Coding Style Guide
    Spacing
    Braces
    Lines
    Keywords
    Bools
    Control Struct

    View Slide

  95. Standards
    PSR-7
    HTTP Message Interface
    Requests Responses

    View Slide

  96. Community

    View Slide

  97. View Slide

  98. Community
    Open
    Inviting
    Genuine

    View Slide

  99. Community
    Twitter
    @phpc
    https://twitter.com/CalEvans
    /lists/phpeople
    Framework / Project

    View Slide

  100. Community
    Mailing List
    http://php.net/mailing-lists.php

    View Slide

  101. Community
    IRC
    Freenode
    #phpc
    ##php

    View Slide

  102. Community
    Blogs
    PlanetPHP
    php[architect]

    View Slide

  103. Community
    Podcasts
    PHP Roundtable
    PHP Townhall
    Voices of the ElePHPant

    View Slide

  104. Community
    Local User Group
    Nearest Large City
    Start It!

    View Slide

  105. Community
    Local User Group
    http://www.meetup.com/PHP-Johannesburg-Meetup-Group/
    Joburg PHP
    http://www.meetup.com/Cape-Town-PHP-Group/
    Capetown PHP

    View Slide

  106. Community
    Conferences
    Hallway Track
    Dinner
    Drinks
    Hotel Common Space

    View Slide

  107. Resources

    View Slide

  108. View Slide

  109. Resources
    PHP.net

    View Slide

  110. Resources
    Modern Frameworks
    Laravel

    Symfony2

    Fuel PHP
    SlimPHP 2

    Aura for PHP

    Silex

    View Slide

  111. Resources
    leanpub.com/

    phptherightway
    PHPtheRightWay.com

    View Slide

  112. Resources
    Securing PHP Apps
    SecuringPhpApps.com

    View Slide

  113. Q/A TIME!
    Ben Edmunds
    @benedmunds
    http://benedmunds.com
    http://SecuringPhpApps.com/
    https://joind.in/talk/fa6de

    View Slide