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

PHP 7.x - past, present, future (WordPress Varn...

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.

Boyan Yordanov

March 31, 2017
Tweet

More Decks by Boyan Yordanov

Other Decks in Programming

Transcript

  1. 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)
  2. 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
  3. 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
  4. Anonymous classes Cheap stubs $stub = new class implements SomeInterface{

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

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

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

    How Badoo saved one million dollars switching to PHP7
  8. 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
  9. NULLABLE TYPES Now - allow null, but require int function

    dummy(?int $a) : ?int { return $a; } dummy(null); // returns null
  10. 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);
  11. 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>"; }
  12. VOID RETURN TYPE function init_save() : void { // This

    will cause an Error return add_action('save_post', 'my_custom_callback'); }
  13. 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'); }
  14. 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());
  15. 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];
  16. 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
  17. CATCHING MULTIPLE EXCEPTIONS try { // Do stuff and throw

    exception } catch (OneException | TwoException $e) { // add some common handling code }
  18. 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 }
  19. 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
  20. 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();
  21. ARGUMENTCOUNTERROR NEGATIVE STRING OFFSETS function countsOnArguments($arg) {} countsOnArguments(); // throws

    ArgumentCountError $phpVarna = 'PHP Varna is cool.' echo $phpVarna[­5]; // prints "c"
  22. 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
  23. 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.
  24. 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 ]);
  25. 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) { /* ... */ } }
  26. 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];
  27. 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