PHP 7.x - past, present, future (WordPress Varna #4)

PHP 7.x - past, present, future (WordPress Varna #4)

An overview of the cool new features in PHP 7, 7.1 and what's known for 7.2. Examples are WordPress related, as much as possible.

Presented at the local WordPress user group in Varna, Bulgaria.

807dcecdfca0e4078d89127cd440c039?s=128

Boyan Yordanov

March 31, 2017
Tweet

Transcript

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

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

    writer Organizer of phpVarna
  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 808 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. SUPPORT Ubuntu 16.04 and up Available via third parties on

    most Linux distros Available on the better shared and wp speci c providers: SiteGround, WP Engine, Bluehost, Superhosting, ICN WordPress.com runs PHP7
  12. FEATURES

  13. Speed and Memory consumption Benchmark (regular page)

  14. RSS feed Source: https://ma.ttias.be/wordpress-php-7-1/

  15. Abstract Syntax Trees (AST)

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

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

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

    declare(strict_types=1); function typesAreNotScary(int $number) : int { return $number; }
  19. MORE INFO Black re.io - optimizations added in PHP 7

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

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

    $a; } dummy(null); // TypeError
  22. 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
  23. NULLABLE TYPES Now - allow null, but require int function

    dummy(?int $a) : ?int { return $a; } dummy(null); // returns null
  24. 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);
  25. function titlebox_shortcode($attrs = [], ?string $content, $tag = { //

    normalize attribute keys, lowercase $attrs = array_change_key_case((array)$attrs, CASE_LOWER); $title = $attrs['title'] ?? 'Default title'; $content = $content ?? 'Default content'; return "<div><h3>$title</h3><p>$content</p></div>"; }
  26. VOID RETURN TYPE function init_save() : void { add_action('save_post', 'my_custom_callback');

    }
  27. VOID RETURN TYPE function init_save() : void { // This

    will cause an Error return add_action('save_post', 'my_custom_callback'); }
  28. VOID RETURN TYPE But you can still `return` to break

    execution function init_save() : void { if($someCondition) { return; } add_action('save_post', 'my_custom_callback'); }
  29. ITERABLE PSEUDO-TYPE function iterateStruct(iterable $array) { foreach($array as $item) {

    // Do stuff with $item } }
  30. 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());
  31. 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];
  32. 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
  33. CATCHING MULTIPLE EXCEPTIONS try { // Do stuff and throw

    exception } catch (OneException | TwoException $e) { // add some common handling code }
  34. 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 }
  35. CLOSURE::FROMCALLABLE() Create closures from callable pseudo-type // Without the new

    method function filter_func() {} add_filter('example_filter', 'filter_func'); // With the new syntax add_filter('example_filter', Closure::fromCallable('filter_func')); // Throws Error add_filter('example_filter', Closure::fromCallable('callback_wrong_name
  36. CLOSURE::FROMCALLABLE() You can use class methods too class FooPlugin ()

    { public function init() { add_filter( 'example_filter', Closure::fromCallable([$this, 'exampleFilterMethod'] ); } private function exampleFilterMethod() {} } (new FooPlugin)­>init();
  37. ARGUMENTCOUNTERROR NEGATIVE STRING OFFSETS function countsOnArguments($arg) {} countsOnArguments(); // throws

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

  39. PHP NEXT CURRENTLY PHP 7.2

  40. 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
  41. 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.
  42. 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 ]);
  43. 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) { /* ... */ } }
  44. 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];
  45. HOW TO STAY ON TOP? You don't have to

  46. 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 @DragonBe @CalEvans PHP Weekly newsletter PRs on PHP-SRC repo on GitHub
  47. QUESTIONS? THANK YOU!