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

Modern PHP, Standards, and Community

Ben Edmunds
January 14, 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

January 14, 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
    Statics
    PDO
    Short Arrays
    Null Coalescing

    View Slide

  7. Legit Tools

    View Slide

  8. Legit Tools
    Built-in Server
    Unit Testing
    Composer

    View Slide

  9. Standards

    View Slide

  10. Standards
    PHP-FIG
    PSRs

    View Slide

  11. Community

    View Slide

  12. Community
    Twitter
    etc
    User Groups

    View Slide

  13. Welcome to
    the Future

    View Slide

  14. Great Scott!

    View Slide

  15. Exceptions

    View Slide

  16. View Slide

  17. Exceptions
    try {

    //your code goes here

    }

    catch (Exception $e) {

    die($e->getMessage());

    }

    View Slide

  18. Exceptions
    try {

    //your code goes here

    }

    catch (Exception $e) {

    die($e->getMessage());

    }

    View Slide

  19. Closures

    View Slide

  20. View Slide

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



    return View::make(‘index');

    });

    View Slide

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



    return View::make(‘index');

    });

    View Slide

  23. Namespaces

    View Slide

  24. View Slide

  25. Namespaces
    namespace Illuminate\Console;

    class Command

    {

    //…

    View Slide

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

    class Command

    {

    //…

    View Slide

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

    class Command

    {

    //…

    View Slide

  28. Statics

    View Slide

  29. View Slide

  30. Statics
    Class Route {

    public static function get() {

    //…

    }

    View Slide

  31. Statics
    Route::get();
    Class Route {

    public static function get() {

    //…

    }

    View Slide

  32. Statics
    Route::get();
    Class Route {

    public static function get() {

    //…

    }

    View Slide

  33. Statics
    NO $this



    $var = self::varAtDefinition;

    $var = static::varAtExec;

    View Slide

  34. Short Array
    Syntax

    View Slide

  35. View Slide

  36. Short Array Syntax
    $array = array(

    0 => ‘value1’,

    1 => ‘value2’,

    );

    View Slide

  37. Short Array Syntax
    $array = [

    0 => ‘value1’,

    1 => ‘value2’,

    ];

    View Slide

  38. Short Array Syntax
    $array = [

    0 => ‘value1’,

    1 => ‘value2’,

    ];

    View Slide

  39. Null
    Coalescing
    Operator

    View Slide

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

    View Slide

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

    View Slide

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

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

    View Slide

  43. Traits

    View Slide

  44. Traits
    // grouping without

    // strict inheritance

    trait baseUser {

    function getName() {

    return ‘Jon Snow’;

    }

    }

    View Slide

  45. Traits
    class adminUser {

    use baseUser;

    }

    View Slide

  46. Traits
    $adminUser = new adminUser;

    echo $adminUser->getName();

    //output = ‘Jon Snow’

    View Slide

  47. PDO

    View Slide

  48. View Slide

  49. PDO
    Cross System

    View Slide

  50. PDO
    Cross System
    MS SQL

    MySQL

    Oracle

    PostgreSQL

    SQLite
    CUBRID

    Firebird

    Informix

    ODBC & DB2

    4D

    View Slide

  51. PDO
    Cross System
    Safe Binding

    View Slide

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

    SELECT * FROM users

    WHERE id=:id

    ’);

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

    $stmt->execute();

    View Slide

  53. Legit Tools

    View Slide

  54. View Slide

  55. Built-in
    Web Server

    View Slide

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

    PHP 5.4.0 Development Server started…
    Listening on localhost:8000

    Document root is /home/ben/htdocs

    Press Ctrl-C to quit

    View Slide

  57. Composer

    View Slide

  58. Another
    Package Manager!?

    View Slide

  59. Composer
    Sane Package

    Management

    View Slide

  60. Composer
    Autoloading

    View Slide

  61. Composer
    PEAR, ha!
    packagist.org

    View Slide

  62. Composer
    / composer.json

    {

    "require": {

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

    "twilio/sdk": "dev-master"

    }

    }

    View Slide

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

    View Slide

  64. Composer
    $client =

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

    ->messages

    ->sendMessage(…)

    View Slide

  65. Unit Testing

    View Slide

  66. View Slide

  67. Unit Testing
    PHPUnit

    Behat

    Mink
    Selenium

    CodeCeption

    PHPSpec

    View Slide

  68. Unit Testing
    class ApiAuthTest extends PHPUnit_Framework_TestCase {

    public function testVerify() {

    $auth = new apiAuth();



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

    View Slide

  69. Unit Testing
    class ApiAuthTest extends PHPUnit_Framework_TestCase {

    public function testVerify() {

    $auth = new apiAuth();



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

    View Slide

  70. Unit Testing
    $ phpunit tests

    PHPUnit 3.3.17 by Sebastian Bergmann.

    Time: 0.01 seconds

    OK (1 tests, 1 assertions)

    View Slide

  71. Standards

    View Slide

  72. View Slide

  73. Standards
    PHP-FIG
    Framework

    Interop

    Group

    View Slide

  74. Standards
    Member Projects
    Zend

    Symfony

    CakePHP
    Laravel

    SugarCRM

    Drupal

    View Slide

  75. Standards
    PSRs
    PHP

    Standards

    Recommendation

    View Slide

  76. Standards
    PSRs
    Autoloading

    Interfaces

    Style Guides

    View Slide

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

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

    View Slide

  79. Standards
    $client =

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

    ->messages

    ->sendMessage(…)

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  84. Standards
    PSR-7
    HTTP Message Interface
    Requests Responses

    View Slide

  85. Community

    View Slide

  86. View Slide

  87. Community
    Open
    Inviting
    Genuine

    View Slide

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

    View Slide

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

    View Slide

  90. Community
    IRC
    Freenode
    #phpc
    ##php

    View Slide

  91. Community
    Blogs
    PlanetPHP
    php[architect]

    View Slide

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

    View Slide

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

    View Slide

  94. Community
    Conferences
    Hallway Track
    Dinner
    Drinks
    Hotel Lobby

    View Slide

  95. Resources

    View Slide

  96. View Slide

  97. Resources
    PHP.net

    View Slide

  98. Resources
    Modern Frameworks
    Laravel

    Symfony2

    Fuel PHP
    SlimPHP 2

    Aura for PHP

    Silex

    View Slide

  99. Resources
    leanpub.com/

    phptherightway
    PHPtheRightWay.com

    View Slide

  100. Resources
    BuildSecurePHPapps.com
    Coupon Code:

    skiphp
    $10 off
    http://buildsecurephpapps.com/?coupon=skiphp

    View Slide

  101. Q/A TIME!
    Ben Edmunds
    @benedmunds
    http://benedmunds.com
    http://buildsecurephpapps.com/?coupon=skiphp
    https://joind.in/talk/aedbd

    View Slide