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 full-size slide

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

    View full-size slide

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

    View full-size slide

  4. Welcome to
    the Future

    View full-size slide

  5. Welcome to the Future
    Exceptions
    Namespaces
    Closures

    View full-size slide

  6. Welcome to the Future
    PDO
    Short Arrays
    Null Coalescing

    View full-size slide

  7. Welcome to the Future
    Scalar Type Hints

    Return Type
    Declarations

    View full-size slide

  8. Legit Tools
    Built-in Server
    Unit Testing
    Composer

    View full-size slide

  9. Standards
    PHP-FIG
    PSRs

    View full-size slide

  10. Community
    Twitter
    etc
    User Groups

    View full-size slide

  11. Welcome to
    the Future

    View full-size slide

  12. Great Scott!

    View full-size slide

  13. Exceptions
    try {

    //your code goes here

    }

    catch (Exception $e) {

    die($e->getMessage());

    }

    View full-size slide

  14. Exceptions
    try {

    //your code goes here

    }

    catch (Exception $e) {

    die($e->getMessage());

    }

    View full-size slide

  15. Errors
    Exception implements Throwable
    Error implements Throwable

    View full-size slide

  16. Errors
    try {

    //error thrown here

    }

    catch (Error $e) {

    die($e->getMessage());

    }

    View full-size slide

  17. Errors
    try {

    //error thrown here

    } catch (Error $e) {

    die($e->getMessage());

    } catch (Exception $e) {

    die($e->getMessage());

    }

    View full-size slide

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



    return View::make(‘index');

    });

    View full-size slide

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



    return View::make(‘index');

    });

    View full-size slide

  20. Namespaces
    namespace Illuminate\Console;

    class Command

    {

    //…

    View full-size slide

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

    class Command

    {

    //…

    View full-size slide

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

    class Command

    {

    //…

    View full-size slide

  23. Short Array
    Syntax

    View full-size slide

  24. Short Array Syntax
    $array = array(

    0 => ‘value1’,

    1 => ‘value2’,

    );

    View full-size slide

  25. Short Array Syntax
    $array = [

    0 => ‘value1’,

    1 => ‘value2’,

    ];

    View full-size slide

  26. Short Array Syntax
    $array = [

    0 => ‘value1’,

    1 => ‘value2’,

    ];

    View full-size slide

  27. Null
    Coalescing
    Operator

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

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

    View full-size slide

  31. Traits
    // grouping without

    // strict inheritance

    trait baseUser {

    function getName() {

    return ‘Jon Snow’;

    }

    }

    View full-size slide

  32. Traits
    class adminUser {

    use baseUser;

    }

    View full-size slide

  33. Traits
    $adminUser = new adminUser;

    echo $adminUser->getName();

    //output = ‘Jon Snow’

    View full-size slide

  34. PDO
    Cross System

    View full-size slide

  35. PDO
    Cross System
    MS SQL

    MySQL

    Oracle

    PostgreSQL

    SQLite
    CUBRID

    Firebird

    Informix

    ODBC & DB2

    4D

    View full-size slide

  36. PDO
    Cross System
    Safe Binding

    View full-size slide

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

    SELECT * FROM users

    WHERE id=:id

    ’);

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

    $stmt->execute();

    View full-size slide

  38. Scalar Type
    Hinting

    View full-size slide

  39. Scalar Types
    declare(strict_types=1);

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

    return $a + $b;

    }

    View full-size slide

  40. 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 full-size slide

  41. 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 full-size slide

  42. Scalar Types
    try {

    addNums(2, "1 week”);

    }

    catch(TypeError $e) {}

    View full-size slide

  43. Scalar Types
    try {

    addNums(2, 1);

    }

    catch(TypeError $e) {}

    //3

    View full-size slide

  44. Scalar Types
    class/interface

    array

    callable

    bool

    float

    int

    string

    View full-size slide

  45. Return Type
    Declarations

    View full-size slide

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

    return $a + $b;

    }

    View full-size slide

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

    return $a + $b;

    }

    View full-size slide

  48. 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 full-size slide

  49. Built-in
    Web Server

    View full-size slide

  50. 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 full-size slide

  51. Another
    Package Manager!?

    View full-size slide

  52. Composer
    Sane Package

    Management

    View full-size slide

  53. Composer
    Autoloading

    View full-size slide

  54. Composer
    PEAR, ha!
    packagist.org

    View full-size slide

  55. Composer
    / composer.json

    {

    "require": {

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

    "twilio/sdk": "dev-master"

    }

    }

    View full-size slide

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

    View full-size slide

  57. Composer
    $client =

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

    ->messages

    ->sendMessage(…)

    View full-size slide

  58. Unit Testing

    View full-size slide

  59. Unit Testing
    PHPUnit

    Behat

    Mink
    Selenium

    CodeCeption

    PHPSpec

    View full-size slide

  60. Unit Testing
    class ApiAuthTest extends PHPUnit_Framework_TestCase {

    public function testVerify() {

    $auth = new apiAuth();



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

    View full-size slide

  61. Unit Testing
    class ApiAuthTest extends PHPUnit_Framework_TestCase {

    public function testVerify() {

    $auth = new apiAuth();



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

    View full-size slide

  62. Unit Testing
    $ phpunit tests

    PHPUnit 3.3.17 by Sebastian Bergmann.

    Time: 0.01 seconds

    OK (1 tests, 1 assertions)

    View full-size slide

  63. Standards
    PHP-FIG
    Framework

    Interop

    Group

    View full-size slide

  64. Standards
    Member Projects
    Zend

    Symfony

    CakePHP
    Laravel

    SugarCRM

    Drupal

    View full-size slide

  65. Standards
    PSRs
    PHP

    Standards

    Recommendation

    View full-size slide

  66. Standards
    PSRs
    Autoloading

    Interfaces

    Style Guides

    View full-size slide

  67. 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 full-size slide

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

    View full-size slide

  69. Standards
    $client =

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

    ->messages

    ->sendMessage(…)

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  74. Standards
    PSR-7
    HTTP Message Interface
    Requests Responses

    View full-size slide

  75. Community
    Open
    Inviting
    Genuine

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  78. Community
    IRC
    Freenode
    #phpc
    ##php

    View full-size slide

  79. Community
    Blogs
    PlanetPHP
    php[architect]

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  82. 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 full-size slide

  83. Community
    Conferences
    Hallway Track
    Dinner
    Drinks
    Hotel Common Space

    View full-size slide

  84. Resources
    PHP.net

    View full-size slide

  85. Resources
    Modern Frameworks
    Laravel

    Symfony2

    Fuel PHP
    SlimPHP 2

    Aura for PHP

    Silex

    View full-size slide

  86. Resources
    leanpub.com/

    phptherightway
    PHPtheRightWay.com

    View full-size slide

  87. Resources
    Securing PHP Apps
    SecuringPhpApps.com

    View full-size slide

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

    View full-size slide