Pro Yearly is on sale from $80 to $50! »

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.

22f21d5c22b930fd35dd98f25dedf6a4?s=128

Ben Edmunds

May 19, 2017
Tweet

Transcript

  1. PHP modern standards & Community

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

  3. Who is this guy? Ben Edmunds Open Source Author PHP

    Town Hall Podcast CTO at Mindfulware
  4. Welcome to the Future

  5. Welcome to the Future Exceptions PDO

  6. Welcome to the Future Closures Namespaces

  7. Welcome to the Future Short Array Syntax Traits Password Hashing

  8. Welcome to the Future Null Coalescing Scalar Type Hints Return

    Type Declarations
  9. Legit Tools

  10. Legit Tools Built-in Server Unit Testing Composer

  11. Standards

  12. Standards PHP-FIG PSRs

  13. Community

  14. Community Twitter etc User Groups

  15. Welcome to the Future

  16. Great Scott!

  17. Exceptions

  18. None
  19. Exceptions try { //your code goes here } catch (Exception

    $e) { die($e->getMessage()); }
  20. Exceptions try { //your code goes here } catch (Exception

    $e) { die($e->getMessage()); }
  21. Errors Exception implements Throwable Error implements Throwable

  22. Errors try { //error thrown here } catch (Error $e)

    { die($e->getMessage()); }
  23. Errors try { //error thrown here } catch (Error $e)

    { die($e->getMessage()); } catch (Exception $e) { die($e->getMessage()); }
  24. Errors try { //err or excpt thrown here } catch

    (Throwable $t) { die($t->getMessage()); }
  25. PDO

  26. None
  27. PDO Cross System

  28. PDO Cross System MS SQL MySQL Oracle PostgreSQL SQLite CUBRID

    Firebird Informix ODBC & DB2 4D
  29. PDO Cross System Safe Binding

  30. PDO $stmt = $db->prepare(‘ SELECT * FROM users WHERE id=:id

    ’); $stmt->bindParam(‘:id’, $id); $stmt->execute();
  31. Closures

  32. None
  33. Closures Route::get(‘/', function(){ return View::make(‘index'); });

  34. Closures Route::get(‘/', function(){ return View::make(‘index'); });

  35. Namespaces

  36. None
  37. Namespaces namespace Illuminate\Console; class Command { //…

  38. Namespaces use Illuminate\Console\Command; namespace Illuminate\Console; class Command { //…

  39. Namespaces use Illuminate\Console\Command; namespace Illuminate\Console; class Command { //…

  40. Short Array Syntax

  41. None
  42. Short Array Syntax $array = array( 0 => ‘value1’, 1

    => ‘value2’, );
  43. Short Array Syntax $array = [ 0 => ‘value1’, 1

    => ‘value2’, ];
  44. Short Array Syntax $array = [ 0 => ‘value1’, 1

    => ‘value2’, ];
  45. Traits

  46. Traits // grouping without // strict inheritance trait baseUser {

    function getName() { return ‘Jon Snow’; } }
  47. Traits class adminUser { use baseUser; }

  48. Traits $adminUser = new adminUser; echo $adminUser->getName(); //output = ‘Jon

    Snow’
  49. Passwords

  50. Passwords //safe password hashing password_hash($_POST['pass'], ALGO);

  51. Passwords //safe password hashing password_hash($_POST['pass'], ALGO); //password verification password_verify($_POST['pass'], $u->pass);

  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);
  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();
  54. Null Coalescing Operator

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

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

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

  58. Types!

  59. None
  60. Scalar Type Hinting

  61. Scalar Types declare(strict_types=1); function addNums(float $a, float $b) { return

    $a + $b; }
  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
  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
  64. Scalar Types try { addNums(2, "1 week”); } catch(TypeError $e)

    {}
  65. Scalar Types try { addNums(2, 1); } catch(TypeError $e) {}

    //3
  66. Scalar Types class/interface array callable bool float int string

  67. Return Type Declarations

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

    return $a + $b; }
  69. Scalar Types function addNums(float $a, float $b) : float {

    return $a + $b; }
  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
  71. Legit Tools

  72. None
  73. Built-in Web Server

  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
  75. Composer

  76. Another Package Manager!?

  77. Composer Sane Package Management

  78. Composer Autoloading

  79. Composer PEAR, ha! packagist.org

  80. Composer / composer.json { "require": { "stripe/stripe-php": "dev-master", "twilio/sdk": "dev-master"

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

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

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

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

  85. Composer $client = new Services_Twilio($sid, $tkn); $client->account ->messages ->sendMessage(…)

  86. Unit Testing

  87. None
  88. Unit Testing PHPUnit Behat Mink Selenium CodeCeption PHPSpec

  89. Unit Testing class ApiAuthTest extends PHPUnit_Framework_TestCase { public function testVerify()

    { $auth = new apiAuth(); $this->assertTrue($auth->verify());
  90. Unit Testing class ApiAuthTest extends PHPUnit_Framework_TestCase { public function testVerify()

    { $auth = new apiAuth(); $this->assertTrue($auth->verify());
  91. Unit Testing $ phpunit tests PHPUnit 3.3.17 by Sebastian Bergmann.

    Time: 0.01 seconds OK (1 tests, 1 assertions)
  92. Standards

  93. None
  94. Standards PHP-FIG Framework Interop Group

  95. Standards Member Projects Zend Symfony CakePHP Magento Joomla Drupal

  96. Standards PSRs PHP Standards Recommendation

  97. Standards PSRs Autoloading Interfaces Style Guides

  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
  99. Standards PSR-4 Auto-loading Replaces PSR-0 Used by Composer

  100. Standards $client = new Services_Twilio($sid, $tkn); $client->account ->messages ->sendMessage(…)

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

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

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

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

    Control Struct
  105. Standards PSR-7 HTTP Message Interface Requests Responses

  106. Community

  107. None
  108. Community Open Inviting Genuine

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

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

  111. Community IRC Freenode #phpc ##php

  112. Community Blogs PlanetPHP php[architect]

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

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

  115. Community Local User Group?

  116. Community Conferences Hallway Track Dinner Drinks Hotel Common Space

  117. Q/A

  118. Resources

  119. None
  120. Resources PHP.net

  121. Resources Modern Frameworks Laravel Symfony SlimPHP Silex

  122. Resources leanpub.com/ phptherightway PHPtheRightWay.com

  123. Resources SecuringPhpApps.com SecuringNodeApps.com

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