Slide 1

Slide 1 text

What's new in PHP8.1

Slide 2

Slide 2 text

About me — ! Christian Leo-Pernold — "#$%&'()*⛰,-. — / @mazedlx — ⭐ github.com/mazedlx — 1 mazedlx.net — 2 gusch.fredl.at 2/38

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

PHP 8.1 will be released on 2021-11-25 will reach EOL on 2023-11-?? 4/38

Slide 5

Slide 5 text

New Features 5/38

Slide 6

Slide 6 text

Enums 1/3 enum Suit { case CLUBS; case DIAMONDS; case HEARTS; case SPADES; } 6/38

Slide 7

Slide 7 text

Enums 2/3 class Card { public function __construct( public Suit $suit, ) {} } $card = new Card(Suit::HEARTS); 7/38

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

First-Class Callable Syntax function add(int $a, int $b): int { /* */ } $foo = add(...); $foo(a: 1, b:2); 9/38

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Pure Intersection Types function count_and_iterate(Iterator&Countable $value) { foreach($value as $val) {} count($value); } 11/38

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

new In Initializers class Controller { public function __construct( private Logger $logger = new NullLogger(), ) {} } 13/38

Slide 14

Slide 14 text

New never Type function dd(mixed $input): never { // dump exit; } 14/38

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

Explicit Octal Integer Literal Notation // Hex 0x // Bin 0b 016 === 0o16; // true 016 === 0O16; // true 18/38

Slide 19

Slide 19 text

New full_path Variable For Directory Uploads 1/3 foo ├── dir1 │ └── file.txt └── dir2 └── file.txt 19/38

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

Values in the full_path array are user-input, and must NEVER be trusted 22/38

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Deprecations And Breaking Changes 24/38

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Passing null to non-nullable arguments of internal functions str_contains("string", null); // 8.1: deprecation warning // 9.0: type error 28/38

Slide 29

Slide 29 text

Autovivification on false $array = false; $array[] = 2; // Automatic conversion of false to array is deprecated 29/38

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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 '; 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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

ICYMI — PHP Internals News (Blog and Podcast) https:// phpinternals.news 35/38

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

One more thing... 37/38

Slide 38

Slide 38 text

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 ([email protected]). 38/38