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.

Ca57a7cfac69ba3abf517470f3770aae?s=128

Jeremy Lindblom

October 12, 2019
Tweet

Transcript

  1. What's New In PHP 7.4?

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

    of azPHP You can find me at @jeremeamia 2
  3. “ 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
  4. 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
  5. Let’s Review Let’s start by remembering features that were added

    in PHP 7.2 and 7.3 5
  6. 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
  7. What’s New? Let’s take a look at some of the

    new features coming next month in PHP 7.4 7
  8. 1. Typed Properties Can we type everything now? I think

    so!
  9. Typed Properties class Model { private array $data; private bool

    $loaded; private string $name; } 9
  10. What Types Can I Use? array int string bool float

    object 10 iterable <ClassName> <InterfaceName> ?<type> callable void
  11. 2. Improved Covariance and Contravariance Yay! Factories with generic return

    types!
  12. 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
  13. 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
  14. 3. Preloading Preloading frameworks into opcache? Nice!

  15. 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
  16. 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
  17. 4. Unpacking in Arrays Making arrays that much more sweet.

  18. 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
  19. 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
  20. 5. Arrow Functions (Short Closures) One-liner array_maps are easy on

    the eyes.
  21. 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
  22. 6. Null Coalescing Assignment More weapons in the war against

    NULL.
  23. Null Coalescing Operator // Way Before $params['greet'] = isset($params['greet']) ?

    $params['greet'] : 'hello'; // Before $params['greet'] = $params['greet'] ?? 'hello'; // After $params['greet'] ??= 'hello'; 23
  24. 7. Weak References For more intentional memory optimizations.

  25. Weak References 25

  26. Weak References $obj = new Thing(); $ref = new WeakRef($obj);

    // Later if ($ref->valid()) { $myObj = $ref->get(); } else { throw new Exception('object destroyed'); } 26
  27. 8. Numeric Literal Separator Better readability for funky numbers.

  28. Numeric Literal Separator $dec = 299_792_458; $hex = 0xFFFF_A974; $bin

    = 0b0101_1111; 28
  29. 9. Exceptions in toString Thank you. Thank you. Thank you.

    Thank you.
  30. Exceptions in __toString class URL { // … public function

    __toString() { throw new Exception('YOLO!'); } } // NOT A FATAL ERROR ANYMORE! 30
  31. 10. New Custom Serialization Methods Do we finally have good/safe

    serialization?
  32. 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
  33. 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
  34. 34 Thanks! Any questions? You can find me at: ◦

    @jeremeamia ◦ azPHP Meetups
  35. Credits Special thanks to all the people who made and

    released these awesome resources for free: ◦ Presentation template by SlidesCarnival ◦ Photographs by Unsplash 35
  36. Resources ◦ https://www.php.net/manual/en/migration74.new-features.php ◦ https://stitcher.io/blog/new-in-php-74 36