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

What's New in PHP 7.4?

What's New in PHP 7.4?

PHP is constantly evolving and getting better and faster. Come learn about the latest features in the language from the past few years and what's coming up very soon in version 7.4.

Jeremy Lindblom

October 12, 2019
Tweet

More Decks by Jeremy Lindblom

Other Decks in Programming

Transcript

  1. Hello! I am Jeremy Lindblom Longtime PHP lover and co-organizer

    of azPHP You can find me at @jeremeamia 2
  2. “ I did not develop the PHP we know today.

    Dozens, if not hundreds of people, developed PHP. I was simply the first developer. —  R A S M U S   L E R D O R F  — 3
  3. Thank You Nikita Popov nikic is an exceptional programmer, and

    has been responsible for so many of PHP’s recent improvements, both in performance & syntax. 4
  4. PHP 7.2 ◦ object type ◦ libsodium ◦ Limited variance

    ◦ Trailing comma (use) ◦ Argon2 hash alg ◦ New Multibyte funcs ◦ Zip ext enhancements Recent Additions PHP 7.3 ◦ Improved here/nowdoc syntax ◦ Trailing comma (params) ◦ Destructuring with references ◦ Multibyte improvements ◦ array_key_first/last() ◦ JSON_THROW_ON_ERROR 6
  5. What’s New? Let’s take a look at some of the

    new features coming next month in PHP 7.4 7
  6. What Types Can I Use? array int string bool float

    object 10 iterable <ClassName> <InterfaceName> ?<type> callable void
  7. Limited Return Type Covariance interface Client {...} class MyClient implements

    Client {...} abstract class ClientFactory { abstract protected function create(): Client; } class MyClientFactory extends ClientFactory { protected function create(): MyClient {...} } 12
  8. Contravariance in Parameters class Mapper { public function map(Iterator $i)

    {...} } class ArrayMapper extends Mapper { public function map(iterable $iter) { return parent::map(iterator_for($iter)); } } 13
  9. Preloading ◦ Part of Opcache ◦ Write a PHP script

    to preload files ◦ Executed once on server startup ◦ All preloaded files are available in memory for ALL requests ◦ Changes made to the source file won't have any effect, until the server is restarted 15
  10. Preloading # In your INI opcache.preload=/path/to/preload.php # In your preload.php

    $files = /* files to preload */; foreach ($files as $file) { opcache_compile_file($file); } 16
  11. Unpacking Arrays $data = ['b', 'c', 'd']; $more = ['a',

    …$data, 'e', 'f']; # Works with Iterators too $other = ['a', …$iterator, 'e']; # But not with Associative Arrays 18
  12. Goodbye iterator_to_array(); # These two lines do the same thing

    $array = iterator_to_array($iter); $array = […$iter]; # Again, it doesn’t work with # associative arrays. 19
  13. Arrow Functions / Short Closures $add = 5; # Before

    array_map(function($num) use ($add) { return $num + $add; }); # After array_map(fn($num) => $num + $add, $numbers); 21
  14. Null Coalescing Operator // Way Before $params['greet'] = isset($params['greet']) ?

    $params['greet'] : 'hello'; // Before $params['greet'] = $params['greet'] ?? 'hello'; // After $params['greet'] ??= 'hello'; 23
  15. Weak References $obj = new Thing(); $ref = new WeakRef($obj);

    // Later if ($ref->valid()) { $myObj = $ref->get(); } else { throw new Exception('object destroyed'); } 26
  16. Exceptions in __toString class URL { // … public function

    __toString() { throw new Exception('YOLO!'); } } // NOT A FATAL ERROR ANYMORE! 30
  17. New Serialization ◦ Old way: ◦ __sleep() and __wakeup() methods

    ◦ Serializable interface ◦ New __serialize() and __unserialize() ◦ Addresses issues with previous implementations ◦ Serializable interface will be deprecated 32
  18. New Custom Serialization Methods class Foo { private $bar; public

    function __serialize(): array { return ['bar' => $this->bar]; } public function __unserialize(array $data) { $this->bar = $data['bar']; } } 33
  19. Credits Special thanks to all the people who made and

    released these awesome resources for free: ◦ Presentation template by SlidesCarnival ◦ Photographs by Unsplash 35