Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHP 7.x - Past, Present Feature

PHP 7.x - Past, Present Feature

We talked about some interesting things that came to php since the release of php 7. Covered the new features in php 7.1 in more detail and took a look at what is known already for php 7.2.

PHP Varna User Group, March 2017

Boyan Yordanov

March 16, 2017
Tweet

More Decks by Boyan Yordanov

Other Decks in Programming

Transcript

  1. PHP 7.X - PAST, PRESENT, FUTURE Boyan Yordanov

  2. ABOUT ME Developer at Shtrak Community fanboy Wannabe speaker and

    writer Helping make this happen
  3. WHERE TO FIND ME? @specter_bg boyanyordanov boyanyordanov

  4. PHP 7 TIMELINE The Past: PHP 5.6 -> PHP 7.0

    The Present: PHP 7.0 -> PHP 7.1 The Future: PHP Next (7.2)
  5. THE PAST

  6. DEATH OF PHP 5 VERSION SUPPORT

  7. 5.6 IS PRACTICALLY ON LIFE SUPPORT AND PROJECTS ARE DROPPING

    IT The force is weak with those who think that “*” is a version constraint. twitter.com/tobySen/status… 7:27 PM ­ 5 Feb 2017 · Oberding, Deutschland 21 43 Sebastian Bergmann @s_bergmann Follow Laravel 5.5 in July 2017 will require PHP 7+ 4:29 PM ­ 16 Dec 2016 419 810 Taylor Otwell @taylorotwell Follow #Symfony 4 (to released in November 2017) will drop #PHP 5 support... So, #Twig 2 stable should probably supports PHP 7 only, right? 2:54 PM ­ 16 Dec 2016 264 383 Fabien Potencier @fabpot Follow For the future 2.6 release I will drop support for PHP < 7.0 to massively simplify code. Should I keep support for PHP 5? 1:22 PM ­ 13 Dec 2016 · City of London, London 39 12 Xdebug @xdebug Follow
  8. None
  9. THE PRESENT

  10. PHP 7 Worldwide celebration

  11. FEATURES

  12. Speed and Memory consumption Abstract Syntax Trees (AST)

  13. Anonymous classes Cheap stubs $stub = new class implements SomeInterface{

    public function methodStub() {} }; $subject = new ClassIamTesting($stub);
  14. Returning objects function objFromArray() { return (object) [ 'prop' =>

    'value'; ]; } // ... function objFromAnnonymous() { retrurn new class { public $prop = 'value'; }; }
  15. Return types Scalar type hints Throwable and Error and more

    declare(strict_types=1)
  16. MORE INFO Black re.io - optimizations added in PHP 7

    How Badoo saved one million dollars switching to PHP7
  17. PHP 7.1 New stuff

  18. NULLABLE TYPES Previously function dummy(int $a) : int { return

    $a; } dummy(null); // TypeError
  19. NULLABLE TYPES Accepts null, but the argument is optional function

    dummy(int $a = null) : int { return $a ?? 0; } dummy(null); // returns 0 dummy(); // still returns 0
  20. NULLABLE TYPES Now - allow null, but require int function

    dummy(?int $a) : ?int { return $a; } dummy(null); // returns null
  21. NULLABLE TYPES A more real use case function sum(?int $a,

    ?int $b) : int { $a = $a ?? 0; $b = $b ?? 0; return $a + $b; } ... // web request / console arguments $a = getFromInput('a'); $b= getFromInput('b'); $sum = sum($a, $b);
  22. VOID RETURN TYPE function noResult() : void { // Do

    stuff }
  23. VOID RETURN TYPE function noResult() : void { return 1;

    // Fatal error }
  24. VOID RETURN TYPE But you can still `return` to break

    execution function noResult() : void { // Do stuff if($someContition) { return; } // Do more stuff }
  25. ITERABLE PSEUDO-TYPE function iterateStruct(iterable $array) { foreach($array as $item) {

    // Do stuff with $item } }
  26. ITERABLE PSEUDO-TYPE Same result with any construct you can iterate

    over $array = ['item 1', 'item 2', 'item 3']; $collection = new class implements Iterator { ... }; function gen() { yield 'item 1'; yield 'item 2'; yield 'item 3'; } iterateStruct($array); iterateStruct($collection); iterateStruct(gen());
  27. LIST() AND ARRAY DESTRUCTURING CHANGES Use keys with list() Short

    syntax list('foo' => $foo, 'baz' =>$baz) = ['foo' => 1, 'bar' => 2]; [$foo, $bar] = [1, 2, 3]; ['foo' => $foo] = ['foo' => 1, 'bar' => 2];
  28. CLASS CONSTANT VISIBILITY Declare private, protected and public constants class

    SpecialConfig { private const MY_VALUE = 'value'; public function doStuff() { echo self::MY_VALUE; } } $special = new SpecialConfig(); $special­>doStuff(); // prints 'value' echo SpecialConfig::MY_VALUE; // throws error
  29. CATCHING MULTIPLE EXCEPTIONS try { // Do stuff and throw

    exception } catch (OneException | TwoException $e) { // add some common handling code }
  30. CATCHING MULTIPLE EXCEPTIONS Without the new thingy try { //

    Do stuff and throw exception } catch (OneException $e) { // do stuff with $e } catch (TwoException $e) { // copy­paste the same code from above }
  31. CLOSURE::FROMCALLABLE() Create closures from callable pseudo-type // Without the new

    method array_map(function($item) {}, $arr); array_map('myCallback', $arr); // With the new syntax array_map(Closure::fromCallable('myCallback'), $arr); // Throws Error array_map(Closure::fromCallable('myCallback_wrong_name'), $arr);
  32. CLOSURE::FROMCALLABLE() You can use class methods too $arr = ['item

    1', 'item 2', 'item 3']; class MyClass { public function methodCallback($item) { echo $item; } } $myCallback = Closure::fromCallable([new MyClass, 'methodCallback' echo get_class($myCallback); // Prints Closure array_map($myCallback, $arr);
  33. ARGUMENTCOUNTERROR NEGATIVE STRING OFFSETS function countsOnArguments($arg) {} countsOnArguments(); // throws

    ArgumentCountError $phpVarna = 'PHP Varna is cool.' echo $phpVarna[­5]; // prints "c"
  34. THE FUTURE

  35. PHP NEXT CURRENTLY PHP 7.2

  36. WHAT DO WE KNOW? No release date yet - probably

    in December again A bunch of things are deprecated New password hashing algorithm in core Libsodium will be in core
  37. LIBSODIUM From the RFC Libsodium is a modern cryptography library

    that offers authenticated encryption, high-speed elliptic curve cryptography, and much more. Unlike other cryptography standards (which are a potluck of cryptography primitives; i.e. WebCrypto), libsodium is comprised of carefully selected algorithms implemented by security experts to avoid side-channel vulnerabilities.
  38. MODERN CRYPTOGRAPHY YES - NAMESPACES - HELL NO! #PHP 7.2

    will be the first programming language to accept modern cryptography into its standard library.news.php.net/php.internals/… 11:09 PM ­ 10 Feb 2017 451 494 Scott Arciszewski @CiPHPerCoder Follow But no namespaces. God help you if we find namespaces. twitter.com/ciphpercoder/s… 2:10 AM ­ 11 Feb 2017 25 SaraMG @SaraMG Follow
  39. ARGON2 HASHING ALGORITHM Uses PASSWORD_ARGON2I in password_* functions Control the

    parameters password_hash('password', PASSWORD_ARGON2I); passowrd_hash('password', PASSWORD_ARGON2I, [ 'memory_cost' => PASSWORD_ARGON2_DEFAULT_MEMORY_COST, 'time_cost' => PASSWORD_ARGON2_DEFAULT_TIME_COST, 'threads' => PASSWORD_ARGON2_DEFAULT_THREADS ]);
  40. PARAMETER TYPE WIDENING Skip type hints when overriding parent class

    methods class ArrayClass { public function foo(array $foo) { /* ... */ } } // This RFC proposes allowing the type // to be widened to be untyped aka any // type can be passed as the parameter. // Any type restrictions can be done // via user code in the method body. class EverythingClass extends ArrayClass { public function foo($foo) { /* ... */ } }
  41. COUNTING OF NON-COUNTABLE OBJECTS Any scalar, null or object not

    implementing Countable will raise a Warning CONVERT NUMERIC KEYS IN OBJECT/ARRAY CASTS Numeric keys are no longer lost in the conversion $obj = (object)['1' => 1, '2' => 2, '3' => 3];
  42. HOW TO STAY ON TOP? You don't have to

  43. NAVIGATING THE INTERNALS NEWS 1. RFCs - the best source,

    sometimes abandoned or postponed a lot 2. The mailing list - old, chaotic, hard to follow 3. - internals mailing list in a nice UI 4. Twitter: and many more, because the community is awesome! 5. Blogs and 6. Externals.io @of cial_php @nikita_ppv @SaraMG PHP Weekly newsletter PRs on PHP-SRC repo on GitHub
  44. QUESTIONS? THANK YOU!