Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

What's new with PHP7

570 views

Published on

PHP7 brings a tremendous number of new features. Tonight, we will take a look at the null coalesce operator, new execution order (uniform variable syntax), new exceptions and more.

Published in: Software
  • Be the first to comment

What's new with PHP7

  1. 1. WELCOME!
  2. 2. MY FAMILY
  3. 3. HONEY BEES
  4. 4. NEW TECHNOLOGY
  5. 5. WITH MUCH GRATITUDE. • Many, many hours represented: • 189 people • 10,033 commits • All to give us a better programming language.
  6. 6. 5+1=
  7. 7. 5+1=7
  8. 8. PHP6?
  9. 9. HTTPS://PHILSTURGEON.UK/ PHP/ 2014/ 07/ 23/ NEVERENDING-MUPPET- DEBATE-OF-PHP-6-V-PHP-7
  10. 10. HTTPS://3V4L.ORG/ “EVAL”
  11. 11. NEW FEATURES UPDATES BREAKING CHANGES IMPLEMENTATION
  12. 12. NULL COALESCEhttps://wiki.php.net/rfc/isset_ternary
  13. 13. $action = isset($_GET[‘action’]) ? $_GET[‘action’] : ‘default’; < PHP 7.0
  14. 14. $action = isset($_GET[‘action’]) ? $_GET[‘action’] : (isset($_POST[‘action’]) ? $_POST[‘action’] : ($this->getAction() ? $this->getAction() : $_’default’ ) );
  15. 15. ??
  16. 16. $_GET[‘value’] ?? $_POST[‘value’];
  17. 17. $key = (string) $key;
 $keyName = (null !== ($alias = $this->getAlias($key))) ? $alias : $key; 
 if (isset($this->params[$keyName])) {
 return $this->params[$keyName];
 } elseif (isset($this->queryParams[$keyName])) {
 return $this->queryParams[$keyName];
 } elseif (isset($this->postParams[$keyName])) {
 return $this->postParams[$keyName];
 }
 return $default;
  18. 18. $key = (string) $key;
 $keyName = $this->getAlias($key) ?? $key; return $this->params[$keyName] ?? $this->queryParams[$keyName] ?? $this->postParams[$keyName]; PHP 7
  19. 19. 10 > 3 LINES. NOT BAD.
  20. 20. IMPROVED STRONG TYPING SUPPORT https://wiki.php.net/rfc/uniform_variable_syntax https://wiki.php.net/rfc/return_types
  21. 21. function calculateShippingTo($postalCode) { $postalCode = (int)$postalCode; } calculateShippingTo(“66048”); < PHP 7
  22. 22. Class/interface name (PHP 5.0) self (PHP 5.0) array (PHP 5.1) callable (PHP 5.4) bool (PHP 7) float (PHP 7) int (PHP 7) string (PHP 7) Source: http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration
  23. 23. function calculateShippingTo(int $postalCode) { // … } calculateShippingTo(“66048”); // $postalCode is cast to an integer PHP 7
  24. 24. function test(string $val) { echo $val; } test(new class {}); // Can’t convert: Fatal error: Uncaught TypeError: Argument 1 passed to test() must be of the type string, object given…
  25. 25. function test(string $val) { echo $val; } test(new class { public function __toString() { return "Hey, PHP[tek]!"; } }); // Outputs: string(14) "Hey, PHP[tek]!"
  26. 26. function calculateShippingTo(int $postalCode) { // … } //** separate file: **// declare(strict_types = 1); calculateShippingTo(“66048”); // Throws: Catchable fatal error: Argument 1 passed to calculateShippingTo() must be of the type integer, string given
  27. 27. FUNCTION RETURN TYPES • Return type goes after the function declaration: function combine($str1, $str2): string { // … } function addProduct(): Product { // … }
  28. 28. function calculateShippingTo(int $postalCode): float { return 5.11; } var_dump(echo calculateShippingTo(“66048”)); // float(5.11) PHP 7
  29. 29. FAST-FORWARD: https://wiki.php.net/rfc/union_types https://wiki.php.net/rfc/typed-properties
  30. 30. SPACESHIP OPERATOR https://wiki.php.net/rfc/combined-comparison-operator
  31. 31. <?php usort($values, function($a, $b) { if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; }); < PHP 7
  32. 32. <=>
  33. 33. SPACESHIP OPERATOR • Works with anything, including arrays. • 0 if equal • 1 if left > right • -1 if left < right echo 6 <=> 6; // Result: 0 echo 7 <=> 6; // Result: 1 echo 6 <=> 7; // Result: -1
  34. 34. <?php usort($values, function($a, $b) { return $a <=> $b; }); PHP 7
  35. 35. HTTPS://3V4L.ORG/ 96Cd4
  36. 36. GROUPED USE DECLARATIONS https://wiki.php.net/rfc/group_use_declarations
  37. 37. use MagentoFrameworkStdlibCookieCookieReaderInterface;
 use MagentoFrameworkStdlibStringUtils;
 use ZendHttpHeaderHeaderInterface;
 use ZendStdlibParameters;
 use ZendStdlibParametersInterface;
 use ZendUriUriFactory;
 use ZendUriUriInterface; < PHP 7
  38. 38. use MagentoFrameworkStdlib{ CookieCookieReaderInterface, StringUtils };
 use ZendHttpHeaderHeaderInterface;
 use ZendStdlib{Parameters, ParametersInterface};
 use ZendUri{UriFactory, UriInterface}; PHP 7
  39. 39. use MagentoFrameworkStdlib{ CookieCookieReaderInterface, StringUtils as Utils };
 use ZendHttpHeaderHeaderInterface;
 use ZendStdlib{Parameters, ParametersInterface};
 use ZendUri{UriFactory, UriInterface}; PHP 7
  40. 40. ANONYMOUS CLASSES https://wiki.php.net/rfc/anonymous_classes
  41. 41. return new class extends Product { function getWeight() { return 5; } }); // code testing, slight modifications // to classes ANONYMOUS CLASSES
  42. 42. CLOSURE::CALL https://wiki.php.net/rfc/closure_apply
  43. 43. $closure = function($context) { return $context->value . "n"; }; $a = new class { public $value = "Hello"; }; $closure($a); // Output: "Hello" PHP 5.3
  44. 44. $closure = function() { return $this->value . "n"; }; $a = new class { public $value = "Hello"; }; $aBound = $closure->bindTo($a); $closure(); // Output: "Hello" < PHP 7
  45. 45. $canVoidOrder = function() { // executed within the $order context return $this->_canVoidOrder(); } $canVoidOrder = $canVoidOrder->bindTo($order, $order); echo $canVoidOrder(); MAGENTO
  46. 46. $closure = function() { return $this->value . "n"; }; $a = new class { public $value = "Hello"; }; $b = new class { public $value = "Test"; }; echo $closure->call($a); // Output: "Hello" echo $closure->call($b); // Output: "Test" PHP 7
  47. 47. GENERATOR DELEGATION https://wiki.php.net/rfc/generator-delegation
  48. 48. function generator1() { for ($i = 0; $i < 5; $i++) { yield $i; } for ($i = 0; $i < generator2(); $i++) { yield $i; } } function generator2() { /** … **/ } < PHP 7
  49. 49. function mergeOutput($gen1, $gen2) { $array1 = iterator_to_array($gen1); $array2 = iterator_to_array($gen2); return array_merge($array1, $array2); } < PHP 7
  50. 50. function generator1() { for ($i=0; $i<5; $i++) { yield $i; } yield from generator2(); } function generator2() { /** … **/ } // array_merge for generators. PHP 7
  51. 51. NULL COALESCE STRONG-TYPE SUPPORT SPACESHIP OPERATOR GROUPED USE DECLARATIONS ANONYMOUS CLASSES CLOSURE::CALL GENERATOR DELEGATION
  52. 52. NEW FEATURES UPDATES BREAKING CHANGES IMPLEMENTATION
  53. 53. PHP7 PERFORMANCE Source: http://www.lornajane.net/posts/2015/php-7-benchmarks
  54. 54. EXECUTION ORDER CHANGES https://wiki.php.net/rfc/uniform_variable_syntax
  55. 55. $$foo[‘bar’]->baz();
  56. 56. EXECUTION ORDER CHANGES • PHP <= 5.6 had a long list of execution order rules: • Most of the time, the parser executed from left to right. • Breaking changes for some frameworks and some code.
  57. 57. $foo->$bar[‘baz’]; {#1 $foo->result; { #2 PHP 5.6 { #1 $result[‘baz’]; { #2 PHP 7 $foo->$bar[‘baz’];
  58. 58. HTTPS://3V4L.ORG/ WN37p 1Obts PHP 5.6 PHP 7
  59. 59. getClass()::CONST_NAME; PHP 7
  60. 60. getClosure()(); PHP 7
  61. 61. EXCEPTIONS • Exceptions now inherit the new Throwable interface.
  62. 62. try {} catch (Exception $e){ /***/ } try {} catch (Throwable $e) { /***/ }
  63. 63. EXCEPTIONS • New Error class for: • ArithmeticError • DivisionByZeroError • AssertionError • ParseError • TypeError
  64. 64. EXCEPTIONS try { $test = 5 % 0; } catch (Error $e) { var_dump($e); } // throws DivisionByZeroError try { $test = “Test”; $test->go(); } catch (Error $e) { var_dump($e); } // throws Error
  65. 65. FILTERED UNSERIALIZATIONS https://wiki.php.net/rfc/secure_unserialize
  66. 66. UNSERIALIZE • Now takes an additional argument: $options • allowed_classes: • false: don’t unserialize any classes • true: unserialize all classes • array: unserialize specified classes
  67. 67. unserialize($value, [ ‘allowed_classes’ => false ]); unserialize($value, [ ‘allowed_classes’ => [ ‘BlueModelsUserModel’ ] ]); PHP 7
  68. 68. NEW FEATURES UPDATES BREAKING CHANGES IMPLEMENTATION
  69. 69. BREAKING CHANGES
  70. 70. BREAKING CHANGES • mysql_/ereg functions removed. • https://pecl.php.net/package/mysql • Calling a non-static function statically. • ASP-style tags
  71. 71. BREAKING CHANGES • list() assigns variables in proper order now: • call_user_method() and call_user_method_array() removed. list($a[], $a[], $a[]) = [1, 2, 3]; // PHP 5.6 output: 3, 2, 1 // PHP 7 output: 1, 2, 3
  72. 72. HTTP://PHP.NET/ MANUAL/ EN/ MIGRATION70.INCOMPATIBLE.PHP
  73. 73. NEW FEATURES UPDATES BREAKING CHANGES IMPLEMENTATION
  74. 74. PHP7 HAS NOT BEEN WIDELY ADOPTED. YET.
  75. 75. 0.7% ADOPTION SO FAR. Source: https://w3techs.com/technologies/details/pl-php/all/all
  76. 76. WE CAN CHANGE THAT.
  77. 77. UPGRADE CONSIDERATIONS • PHP7 is production-ready. • Few have adopted it. • If you release, you may encounter issues that haven’t been discussed online.
  78. 78. UPGRADE CONSIDERATIONS • What removed functions does your code depend on? • https://github.com/sstalle/php7cc • Run your unit tests against your code on a box with PHP7. • Try to release on a new server: • Will give you a backup if something major happens.
  79. 79. • puphpet.com: choose PHP 7 as the version to install.
 • https://www.digitalocean.com/community/tutorials/ how-to-upgrade-to-php-7-on-centos-7
 • https://www.digitalocean.com/community/tutorials/ how-to-upgrade-to-php-7-on-ubuntu-14-04 INSTALLING PHP7
  80. 80. FURTHER READING • https://leanpub.com/php7/read • Excellent read about every new PHP 7 feature. • http://php.net/manual/en/migration70.new- features.php • PHP’s documentation
  81. 81. TAKE-AWAYS • Take advantage of the new features. • Upgrade. • Encourage your friends about PHP7. • Together, we will get the industry moved to PHP7!
  82. 82. THANK YOU!

×