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

    View full-size slide

  7. Legit Tools
    Built-in Server
    Unit Testing
    Composer

    View full-size slide

  8. Standards
    PHP-FIG
    PSRs

    View full-size slide

  9. Community
    Twitter
    etc
    User Groups

    View full-size slide

  10. Welcome to
    the Future

    View full-size slide

  11. Great Scott!

    View full-size slide

  12. Exceptions
    try {

    //your code goes here

    }

    catch (Exception $e) {

    die($e->getMessage());

    }

    View full-size slide

  13. Exceptions
    try {

    //your code goes here

    }

    catch (Exception $e) {

    die($e->getMessage());

    }

    View full-size slide

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



    return View::make(‘index');

    });

    View full-size slide

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



    return View::make(‘index');

    });

    View full-size slide

  16. Namespaces
    namespace Illuminate\Console;

    class Command

    {

    //…

    View full-size slide

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

    class Command

    {

    //…

    View full-size slide

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

    class Command

    {

    //…

    View full-size slide

  19. Statics
    Class Route {

    public static function get() {

    //…

    }

    View full-size slide

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

    public static function get() {

    //…

    }

    View full-size slide

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

    public static function get() {

    //…

    }

    View full-size slide

  22. Statics
    NO $this



    $var = self::varAtDefinition;

    $var = static::varAtExec;

    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. Built-in
    Web Server

    View full-size slide

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

  40. Another
    Package Manager!?

    View full-size slide

  41. Composer
    Sane Package

    Management

    View full-size slide

  42. Composer
    Autoloading

    View full-size slide

  43. Composer
    PEAR, ha!
    packagist.org

    View full-size slide

  44. Composer
    / composer.json

    {

    "require": {

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

    "twilio/sdk": "dev-master"

    }

    }

    View full-size slide

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

    View full-size slide

  46. Composer
    $client =

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

    ->messages

    ->sendMessage(…)

    View full-size slide

  47. Unit Testing

    View full-size slide

  48. Unit Testing
    PHPUnit

    Behat

    Mink
    Selenium

    CodeCeption

    PHPSpec

    View full-size slide

  49. Unit Testing
    class ApiAuthTest extends PHPUnit_Framework_TestCase {

    public function testVerify() {

    $auth = new apiAuth();



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

    View full-size slide

  50. Unit Testing
    class ApiAuthTest extends PHPUnit_Framework_TestCase {

    public function testVerify() {

    $auth = new apiAuth();



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

    View full-size slide

  51. Unit Testing
    $ phpunit tests

    PHPUnit 3.3.17 by Sebastian Bergmann.

    Time: 0.01 seconds

    OK (1 tests, 1 assertions)

    View full-size slide

  52. Standards
    PHP-FIG
    Framework

    Interop

    Group

    View full-size slide

  53. Standards
    Member Projects
    Zend

    Symfony

    CakePHP
    Laravel

    SugarCRM

    Drupal

    View full-size slide

  54. Standards
    PSRs
    PHP

    Standards

    Recommendation

    View full-size slide

  55. Standards
    PSRs
    Autoloading

    Interfaces

    Style Guides

    View full-size slide

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

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

    View full-size slide

  58. Standards
    $client =

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

    ->messages

    ->sendMessage(…)

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  63. Standards
    PSR-7
    HTTP Message Interface
    Requests Responses

    View full-size slide

  64. Community
    Open
    Inviting
    Genuine

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  67. Community
    IRC
    Freenode
    #phpc
    ##php

    View full-size slide

  68. Community
    Blogs
    PlanetPHP
    php[architect]

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  71. Community
    Conferences
    Hallway Track
    Dinner
    Drinks
    Hotel Lobby

    View full-size slide

  72. Resources
    PHP.net

    View full-size slide

  73. Resources
    Modern Frameworks
    Laravel

    Symfony2

    Fuel PHP
    SlimPHP 2

    Aura for PHP

    Silex

    View full-size slide

  74. Resources
    leanpub.com/

    phptherightway
    PHPtheRightWay.com

    View full-size slide

  75. Resources
    BuildSecurePHPapps.com
    Coupon Code:

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

    View full-size slide

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

    View full-size slide