Modern PHP, Standards, and Community

22f21d5c22b930fd35dd98f25dedf6a4?s=47 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.

22f21d5c22b930fd35dd98f25dedf6a4?s=128

Ben Edmunds

September 29, 2016
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 Namespaces Closures

  6. Welcome to the Future PDO Short Arrays Null Coalescing

  7. Welcome to the Future Scalar Type Hints Return Type Declarations

  8. Legit Tools

  9. Legit Tools Built-in Server Unit Testing Composer

  10. Standards

  11. Standards PHP-FIG PSRs

  12. Community

  13. Community Twitter etc User Groups

  14. Welcome to the Future

  15. Great Scott!

  16. Exceptions

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

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

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

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

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

    { die($e->getMessage()); } catch (Exception $e) { die($e->getMessage()); }
  23. Closures

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

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

  27. Namespaces

  28. None
  29. Namespaces namespace Illuminate\Console; class Command { //…

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

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

  32. Short Array Syntax

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

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

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

    => ‘value2’, ];
  37. Null Coalescing Operator

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

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

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

  41. Traits

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

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

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

    Snow’
  45. PDO

  46. None
  47. PDO Cross System

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

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

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

    ’); $stmt->bindParam(‘:id’, $id); $stmt->execute();
  51. Types!

  52. None
  53. Scalar Type Hinting

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

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

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

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

  60. Return Type Declarations

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

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

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

  65. None
  66. Built-in Web Server

  67. 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
  68. Composer

  69. Another Package Manager!?

  70. Composer Sane Package Management

  71. Composer Autoloading

  72. Composer PEAR, ha! packagist.org

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

    } }
  74. Composer $ php composer.phar update $ php composer.phar install

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

  76. Unit Testing

  77. None
  78. Unit Testing PHPUnit Behat Mink Selenium CodeCeption PHPSpec

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

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

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

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

  83. None
  84. Standards PHP-FIG Framework Interop Group

  85. Standards Member Projects Zend Symfony CakePHP Laravel SugarCRM Drupal

  86. Standards PSRs PHP Standards Recommendation

  87. Standards PSRs Autoloading Interfaces Style Guides

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

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

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

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

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

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

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

  96. Community

  97. None
  98. Community Open Inviting Genuine

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

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

  101. Community IRC Freenode #phpc ##php

  102. Community Blogs PlanetPHP php[architect]

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

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

  105. Community Local User Group http://www.meetup.com/PHP-Johannesburg-Meetup-Group/ Joburg PHP http://www.meetup.com/Cape-Town-PHP-Group/ Capetown PHP

  106. Community Conferences Hallway Track Dinner Drinks Hotel Common Space

  107. Resources

  108. None
  109. Resources PHP.net

  110. Resources Modern Frameworks Laravel Symfony2 Fuel PHP SlimPHP 2 Aura

    for PHP Silex
  111. Resources leanpub.com/ phptherightway PHPtheRightWay.com

  112. Resources Securing PHP Apps SecuringPhpApps.com

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