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

What's new in PHP8.1

What's new in PHP8.1

E157a71a8b1585e6a33e2c6da01d4cac?s=128

Christian Leo-Pernold

September 22, 2021
Tweet

Transcript

  1. What's new in PHP8.1

  2. About me — ! Christian Leo-Pernold — "#$%&'()*⛰,-. — /

    @mazedlx — ⭐ github.com/mazedlx — 1 mazedlx.net — 2 gusch.fredl.at 2/38
  3. Version History and EOL 1 5.6 2018-12-31 (2 years 8

    months ago) 7.0 2019-01-19 (2 years 7 months ago) 7.1 2019-12-31 (1 years 8 months ago) 7.2 2020-11-30 (9 months ago) 7.3 2021-12-06 (3 months from now) 7.4 2022-11-28 (1 year 2 months from now) 8.0 2023-11-26 (2 years 2 months from now) 1 https://en.wikipedia.org/wiki/PHP 3/38
  4. PHP 8.1 will be released on 2021-11-25 will reach EOL

    on 2023-11-?? 4/38
  5. New Features 5/38

  6. Enums 1/3 enum Suit { case CLUBS; case DIAMONDS; case

    HEARTS; case SPADES; } 6/38
  7. Enums 2/3 class Card { public function __construct( public Suit

    $suit, ) {} } $card = new Card(Suit::HEARTS); 7/38
  8. Enums 3/3 enum Suits { /* ... */ public function

    symbol(): string { return match($this) { Suits::CLUBS => ' ♣ ', Suits::DIAMONDS => ' ♦ ', Suits::HEARTS => ' ♥ ', Suits::SPADES => ' ♠ ', }; } } $suit = Suits::HEARTS; $suit->symbol(); // ♥ 8/38
  9. First-Class Callable Syntax function add(int $a, int $b): int {

    /* */ } $foo = add(...); $foo(a: 1, b:2); 9/38
  10. Array Unpacking With String Keys $array1 = ["a" => 1];

    $array2 = ["b" => 2]; $array = ["a" => 0, ...$array1, ...$array2]; var_dump($array); // ["a" => 1, "b" => 2] 10/38
  11. Pure Intersection Types function count_and_iterate(Iterator&Countable $value) { foreach($value as $val)

    {} count($value); } 11/38
  12. Readonly Properties class Post { public function __construct( public readonly

    string $title, public readonly DateTimeImmutable $publishedAt, ) {} } $post = new Post('Title', /* … */); $post->title = 'Other title'; Error: Cannot modify readonly property Post::$title 12/38
  13. new In Initializers class Controller { public function __construct( private

    Logger $logger = new NullLogger(), ) {} } 13/38
  14. New never Type function dd(mixed $input): never { // dump

    exit; } 14/38
  15. New array_is_list Function $list = ["a", "b", "c"]; array_is_list($list); //

    true $notAList = [1 => "a", 2 => "b", 3 => "c"]; array_is_list($notAList); // false $alsoNotAList = ["a" => "a", "b" => "b", "c" => "c"]; array_is_list($alsoNotAList); // false 15/38
  16. Final Class Constants class Foo { final public const E

    = 2.71828; } class Bar extends Foo { public const E = 3.0; // Fatal error: Bar::X cannot override final constant Foo::X } 16/38
  17. New fsync/fdatasync Function $file = fopen("sample.txt", "w"); fwrite($file, "Some content");

    if (fsync($file)) { echo "File has been successfully persisted to disk."; } fclose($file); 17/38
  18. Explicit Octal Integer Literal Notation // Hex 0x // Bin

    0b 016 === 0o16; // true 016 === 0O16; // true 18/38
  19. New full_path Variable For Directory Uploads 1/3 foo ├── dir1

    │ └── file.txt └── dir2 └── file.txt 19/38
  20. New full_path Variable For Directory Uploads 2/3 var_dump($_FILES); // PHP

    < 8.1 array(1) { ["files"]=> array(6) { ["name"]=> array(2) { [0]=> string(8) "file.txt" [1]=> string(8) "file.txt" } ["tmp_name"]=> array(2) { [0]=> string(14) "/tmp/phpV1J3EM" [1]=> string(14) "/tmp/phpzBmAkT" } // ... + error, type, size } } 20/38
  21. New full_path Variable For Directory Uploads 3/3 var_dump($_FILES); // PHP

    >= 8.1 array(1) { ["myupload"]=> array(6) { ["name"]=> array(2) { [0]=> string(8) "file.txt" [1]=> string(8) "file.txt" } ["full_path"]=> array(2) { [0]=> string(19) "foo/dir1/file.txt" [1]=> string(19) "foo/dir2/file.txt" } ["tmp_name"]=> array(2) { [0]=> string(14) "/tmp/phpV1J3EM" [1]=> string(14) "/tmp/phpzBmAkT" } // ... + error, type, size } } 21/38
  22. Values in the full_path array are user-input, and must NEVER

    be trusted 22/38
  23. Fibers — PHP is now asynchronous. Kinda.3 In essence, a

    Fiber is a code block that maintains its stack (variables and state), that can be started, suspended, or terminated cooperatively by the main code and the Fiber4 4 https://php.watch/versions/8.1/fibers 3 https://stitcher.io/blog/fibers-with-a-grain-of-salt 23/38
  24. Deprecations And Breaking Changes 24/38

  25. Resource To Object Migrations These changes are part of the

    long-term vision to convert all resources to dedicated objects.5 finfo_file, finfo_open will use finfo objects instead of resources. 5 https://github.com/php/php-tasks/issues/6 25/38
  26. Restrict $GLOBALS Usage 1/2 What is no longer supported are

    writes to $GLOBALS taken as a whole. All the following will generate a compile-time error — Nikita Popov $GLOBALS = []; $GLOBALS += []; $GLOBALS =& $x; $x =& $GLOBALS; unset($GLOBALS); // Compile-time error by_ref($GLOBALS); // Run-time error 26/38
  27. Restrict $GLOBALS Usage 2/2 Nikita analysed the top 2000 packages

    on Packagist, and only found 23 cases that will be affected by this change. We can conclude the impact of this — technically breaking — change will be low, which is why internals decided to add it in PHP 8.1. Remember that most of us will win by this change, given the positive performance impact it has everywhere in our code.6 6 https://stitcher.io/blog/new-in-php-81 27/38
  28. Passing null to non-nullable arguments of internal functions str_contains("string", null);

    // 8.1: deprecation warning // 9.0: type error 28/38
  29. Autovivification on false $array = false; $array[] = 2; //

    Automatic conversion of false to array is deprecated 29/38
  30. Other Minor Changes 1/3 — MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH no longer has an

    effect — MYSQLI_STORE_RESULT_COPY_DATA no longer has an effect — PDO::ATTR_STRINGIFY_FETCHES now also works with booleans 30/38
  31. Other Minor Changes 2/3 — Integers and floats in PDO

    MySQL and Sqlite result sets will be returned using native PHP types instead of strings when using emulated prepared statements — Functions like htmlspecialchars() and htmlentities() now also escape ' by default to &#039;; malformed UTF-8 will also be replaced with a unicode character, instead of resulting in an empty string — date_sunrise() and date_sunset() functions, along with their INI values, are deprecated in favor of date_sun_info() function 31/38
  32. Other Minor Changes 3/3 — The hash(), hash_file() and hash_init()

    functions have an extra argument added to them called $options with a default value of [] so it won't affect your code — New support for MurmurHash3 and xxHash 32/38
  33. Performance Improvements Between a 5% and 8% performance increase in

    comparison to 8.0.7 — Dmitry Stogov 7 https://github.com/php/php-src/pull/6627#issuecomment-773922448 33/38
  34. Where to go from here? — Read the blog post

    https://www.stitcher.io/blog/new-in- php-81 — Also https://php.watch/versions/8.1 — Subscribe to The Road to PHP 8.1 https://road-to-php.com — Follow @brendt_gd on Twitter https://twitter.com/ brendt_gd — Wait for November 25th, 2021 — Download the developer preview (8.1.0RC2) 34/38
  35. ICYMI — PHP Internals News (Blog and Podcast) https:// phpinternals.news

    35/38
  36. Thanks! Slides can be found at speakerdeck.com/mazedlx 36/38

  37. One more thing... 37/38

  38. We (Arbeiterkammer Wien) are looking for a Fullstack-Developer — Transform

    an existing CodeIgniter 3/jQuery/Bootstrap ( ) application to Laravel ( ) — Freelance on an hourly basis or part-time employment (16 hrs/week) — Work with me — PHP (Laravel, TALL Stack) — JS (Alpine.js) — Git — TDD (PHPUnit and/or Pest) — MySQL — Metal — Beer — Food Hit me up a!erwards and we'll talk shop. Or write me an email (mazedlx@gmail.com). 38/38