$30 off During Our Annual Pro Sale. View Details »

Modern PHP, Standards, and Community (phpDay 2017)

Modern PHP, Standards, and Community (phpDay 2017)

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

May 19, 2017
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

    PDO

    View Slide

  6. Welcome to the Future
    Closures

    Namespaces

    View Slide

  7. Welcome to the Future
    Short Array Syntax

    Traits

    Password Hashing

    View Slide

  8. Welcome to the Future
    Null Coalescing

    Scalar Type Hints

    Return Type Declarations

    View Slide

  9. Legit Tools

    View Slide

  10. Legit Tools
    Built-in Server
    Unit Testing
    Composer

    View Slide

  11. Standards

    View Slide

  12. Standards
    PHP-FIG
    PSRs

    View Slide

  13. Community

    View Slide

  14. Community
    Twitter
    etc
    User Groups

    View Slide

  15. Welcome to
    the Future

    View Slide

  16. Great Scott!

    View Slide

  17. Exceptions

    View Slide

  18. View Slide

  19. Exceptions
    try {

    //your code goes here

    }

    catch (Exception $e) {

    die($e->getMessage());

    }

    View Slide

  20. Exceptions
    try {

    //your code goes here

    }

    catch (Exception $e) {

    die($e->getMessage());

    }

    View Slide

  21. Errors
    Exception implements Throwable
    Error implements Throwable

    View Slide

  22. Errors
    try {

    //error thrown here

    }

    catch (Error $e) {

    die($e->getMessage());

    }

    View Slide

  23. Errors
    try {

    //error thrown here

    } catch (Error $e) {

    die($e->getMessage());

    } catch (Exception $e) {

    die($e->getMessage());

    }

    View Slide

  24. Errors
    try {

    //err or excpt thrown here

    } catch (Throwable $t) {

    die($t->getMessage());

    }

    View Slide

  25. PDO

    View Slide

  26. View Slide

  27. PDO
    Cross System

    View Slide

  28. PDO
    Cross System
    MS SQL

    MySQL

    Oracle

    PostgreSQL

    SQLite
    CUBRID

    Firebird

    Informix

    ODBC & DB2

    4D

    View Slide

  29. PDO
    Cross System
    Safe Binding

    View Slide

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

    SELECT * FROM users

    WHERE id=:id

    ’);

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

    $stmt->execute();

    View Slide

  31. Closures

    View Slide

  32. View Slide

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



    return View::make(‘index');

    });

    View Slide

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



    return View::make(‘index');

    });

    View Slide

  35. Namespaces

    View Slide

  36. View Slide

  37. Namespaces
    namespace Illuminate\Console;

    class Command

    {

    //…

    View Slide

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

    class Command

    {

    //…

    View Slide

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

    class Command

    {

    //…

    View Slide

  40. Short Array
    Syntax

    View Slide

  41. View Slide

  42. Short Array Syntax
    $array = array(

    0 => ‘value1’,

    1 => ‘value2’,

    );

    View Slide

  43. Short Array Syntax
    $array = [

    0 => ‘value1’,

    1 => ‘value2’,

    ];

    View Slide

  44. Short Array Syntax
    $array = [

    0 => ‘value1’,

    1 => ‘value2’,

    ];

    View Slide

  45. Traits

    View Slide

  46. Traits
    // grouping without

    // strict inheritance

    trait baseUser {

    function getName() {

    return ‘Jon Snow’;

    }

    }

    View Slide

  47. Traits
    class adminUser {

    use baseUser;

    }

    View Slide

  48. Traits
    $adminUser = new adminUser;

    echo $adminUser->getName();

    //output = ‘Jon Snow’

    View Slide

  49. Passwords

    View Slide

  50. Passwords
    //safe password hashing

    password_hash($_POST['pass'], ALGO);

    View Slide

  51. Passwords
    //safe password hashing

    password_hash($_POST['pass'], ALGO);
    //password verification

    password_verify($_POST['pass'], $u->pass);

    View Slide

  52. Passwords
    //safe password hashing

    password_hash($_POST['pass'], ALGO);
    //password verification

    password_verify($_POST['pass'], $u->pass);
    //password rehashing

    password_needs_rehash($u->pass, ALGO);

    View Slide

  53. Passwords
    if (password_verify($_POST['pass'], $u->pass)) {

    if (password_needs_rehash(

    $u->pass,

    PASSWORD_DEFAULT

    )) {

    $u->pass = password_hash(

    $_POST['pass'],

    PASSWORD_DEFAULT

    );

    $u->save();

    View Slide

  54. Null
    Coalescing
    Operator

    View Slide

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

    View Slide

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

    View Slide

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

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

    View Slide

  58. Types!

    View Slide

  59. View Slide

  60. Scalar Type
    Hinting

    View Slide

  61. Scalar Types
    declare(strict_types=1);

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

    return $a + $b;

    }

    View Slide

  62. 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

  63. 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

  64. Scalar Types
    try {

    addNums(2, "1 week”);

    }

    catch(TypeError $e) {}

    View Slide

  65. Scalar Types
    try {

    addNums(2, 1);

    }

    catch(TypeError $e) {}

    //3

    View Slide

  66. Scalar Types
    class/interface

    array

    callable

    bool

    float

    int

    string

    View Slide

  67. Return Type
    Declarations

    View Slide

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

    return $a + $b;

    }

    View Slide

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

    return $a + $b;

    }

    View Slide

  70. 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

  71. Legit Tools

    View Slide

  72. View Slide

  73. Built-in
    Web Server

    View Slide

  74. 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

  75. Composer

    View Slide

  76. Another
    Package Manager!?

    View Slide

  77. Composer
    Sane Package

    Management

    View Slide

  78. Composer
    Autoloading

    View Slide

  79. Composer
    PEAR, ha!
    packagist.org

    View Slide

  80. Composer
    / composer.json

    {

    "require": {

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

    "twilio/sdk": "dev-master"

    }

    }

    View Slide

  81. Composer
    $ php composer.phar install
    -> composer.lock

    View Slide

  82. Composer
    $ php composer.phar install
    -> composer.lock
    Commit it ^

    View Slide

  83. Composer
    $ php composer.phar install
    /composer.lock

    View Slide

  84. Composer
    $ php composer.phar update
    /composer.json

    View Slide

  85. Composer
    $client =

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

    ->messages

    ->sendMessage(…)

    View Slide

  86. Unit Testing

    View Slide

  87. View Slide

  88. Unit Testing
    PHPUnit

    Behat

    Mink
    Selenium

    CodeCeption

    PHPSpec

    View Slide

  89. Unit Testing
    class ApiAuthTest extends PHPUnit_Framework_TestCase {

    public function testVerify() {

    $auth = new apiAuth();



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

    View Slide

  90. Unit Testing
    class ApiAuthTest extends PHPUnit_Framework_TestCase {

    public function testVerify() {

    $auth = new apiAuth();



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

    View Slide

  91. Unit Testing
    $ phpunit tests

    PHPUnit 3.3.17 by Sebastian Bergmann.

    Time: 0.01 seconds

    OK (1 tests, 1 assertions)

    View Slide

  92. Standards

    View Slide

  93. View Slide

  94. Standards
    PHP-FIG
    Framework

    Interop

    Group

    View Slide

  95. Standards
    Member Projects
    Zend

    Symfony

    CakePHP
    Magento

    Joomla

    Drupal

    View Slide

  96. Standards
    PSRs
    PHP

    Standards

    Recommendation

    View Slide

  97. Standards
    PSRs
    Autoloading

    Interfaces

    Style Guides

    View Slide

  98. 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

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

    View Slide

  100. Standards
    $client =

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

    ->messages

    ->sendMessage(…)

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  105. Standards
    PSR-7
    HTTP Message Interface
    Requests Responses

    View Slide

  106. Community

    View Slide

  107. View Slide

  108. Community
    Open
    Inviting
    Genuine

    View Slide

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

    View Slide

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

    View Slide

  111. Community
    IRC
    Freenode
    #phpc
    ##php

    View Slide

  112. Community
    Blogs
    PlanetPHP
    php[architect]

    View Slide

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

    View Slide

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

    View Slide

  115. Community
    Local User Group?

    View Slide

  116. Community
    Conferences
    Hallway Track
    Dinner
    Drinks
    Hotel Common Space

    View Slide

  117. Q/A

    View Slide

  118. Resources

    View Slide

  119. View Slide

  120. Resources
    PHP.net

    View Slide

  121. Resources
    Modern Frameworks
    Laravel

    Symfony
    SlimPHP

    Silex

    View Slide

  122. Resources
    leanpub.com/

    phptherightway
    PHPtheRightWay.com

    View Slide

  123. Resources
    SecuringPhpApps.com SecuringNodeApps.com

    View Slide

  124. Thanks!
    Ben Edmunds
    @benedmunds
    http://benedmunds.com
    https://joind.in/talk/44ebe

    View Slide