$30 off During Our Annual Pro Sale. View Details »

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. What's New
    In PHP 7.4?

    View Slide

  2. Hello!
    I am Jeremy Lindblom
    Longtime PHP lover and co-organizer of azPHP
    You can find me at @jeremeamia
    2

    View Slide


  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

    View Slide

  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

    View Slide

  5. Let’s Review
    Let’s start by remembering features
    that were added in PHP 7.2 and 7.3
    5

    View Slide

  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

    View Slide

  7. What’s New?
    Let’s take a look at some of the new
    features coming next month in PHP 7.4
    7

    View Slide

  8. 1.
    Typed Properties
    Can we type everything now? I think so!

    View Slide

  9. Typed Properties
    class Model {
    private array $data;
    private bool $loaded;
    private string $name;
    }
    9

    View Slide

  10. What Types Can I Use?
    array
    int
    string
    bool
    float
    object
    10
    iterable


    ?
    callable
    void

    View Slide

  11. 2.
    Improved Covariance
    and Contravariance
    Yay! Factories with generic return types!

    View Slide

  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

    View Slide

  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

    View Slide

  14. 3.
    Preloading
    Preloading frameworks into opcache? Nice!

    View Slide

  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

    View Slide

  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

    View Slide

  17. 4.
    Unpacking in Arrays
    Making arrays that much more sweet.

    View Slide

  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

    View Slide

  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

    View Slide

  20. 5.
    Arrow Functions
    (Short Closures)
    One-liner array_maps are easy on the eyes.

    View Slide

  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

    View Slide

  22. 6.
    Null Coalescing
    Assignment
    More weapons in the war against NULL.

    View Slide

  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

    View Slide

  24. 7.
    Weak References
    For more intentional memory optimizations.

    View Slide

  25. Weak References
    25

    View Slide

  26. Weak References
    $obj = new Thing();
    $ref = new WeakRef($obj);
    // Later
    if ($ref->valid()) {
    $myObj = $ref->get();
    } else {
    throw new Exception('object destroyed');
    }
    26

    View Slide

  27. 8.
    Numeric Literal
    Separator
    Better readability for funky numbers.

    View Slide

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

    View Slide

  29. 9.
    Exceptions in toString
    Thank you. Thank you. Thank you. Thank you.

    View Slide

  30. Exceptions in __toString
    class URL {
    // …
    public function __toString()
    {
    throw new Exception('YOLO!');
    }
    }
    // NOT A FATAL ERROR ANYMORE!
    30

    View Slide

  31. 10.
    New Custom
    Serialization Methods
    Do we finally have good/safe serialization?

    View Slide

  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

    View Slide

  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

    View Slide

  34. 34
    Thanks!
    Any questions?
    You can find me at:
    ◦ @jeremeamia
    ◦ azPHP Meetups

    View Slide

  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

    View Slide

  36. Resources
    ◦ https://www.php.net/manual/en/migration74.new-features.php
    ◦ https://stitcher.io/blog/new-in-php-74
    36

    View Slide