PHP 8.1 programming language release

After a year of development, the release of the PHP 8.1 programming language is presented. The new branch includes a series of new features as well as several interoperability changes.

Key improvements in PHP 8.1:

  • Support for enumerations has been added, for example, now you can use the following constructs: enum Status { case Pending; case Active; case Archived; } class Post { public function __construct( private Status $status = Status::Pending; ) {} public function setStatus(Status $status): void { // … } } $post->setStatus(Status::Active);
  • Added support for lightweight threads called fibers (Fiber) and allows you to control the execution threads at a low level. Fiber support is planned to be added to the Amphp and ReactPHP frameworks. $fiber = new Fiber(function (): void { $valueAfterResuming = Fiber::suspend('after suspending'); // … }); $valueAfterSuspending = $fiber->start(); $fiber->resume('after resuming');
  • The implementation of the object code cache (opcache) has been improved, in which it became possible to cache information about the inheritance of classes. Optimization made it possible to increase the performance of some applications by 5-8%. Other optimizations include JIT optimization, implementation of JIT support for the ARM64 (AArch64) architecture, faster name resolution, optimization of the timelib and ext/date libraries, improved performance of serialization and deserialization, optimization of the get_declared_classes(), explode(), strtr() functions, strnatcmp(), dechex(). Overall, there is a 23.0% performance improvement for Symfony Demo and 3.5% for WordPress.
  • The unpacking operator inside "...$var" arrays, which allows substitution of existing arrays when defining a new array, has been extended with support for string keys (previously only numeric identifiers were supported). For example, now you can use in code: $array1 = ["a" => 1]; $array2 = ["b" => 2]; $array = ["a" => 0, ...$array1, ...$array2]; var_dump($array); // ["a" => 1, "b" => 2]
  • It is allowed to use the "new" keyword in initializers, such as in function definitions as a default parameter or in argument attributes. class MyController { public function __construct( private Logger $logger = new NullLogger(), ) {} }
  • The ability to mark class properties for read-only access is provided (information in such properties can be written only once, after which it will not be available for change). class PostData { public function __construct( public readonly string $title, public readonly DateTimeImmutable $date, ) {} } $post = new Post('Title', /* … */); $post->title = 'Other'; > Error: Cannot modify readonly property Post::$title
  • A new syntax for callable objects has been implemented - a closure can now be formed by calling a function and passing it the value β€œβ€¦β€ as an argument (i.e. myFunc(…) instead of Closure::fromCallable('myFunc')): function foo (int $a, int $b) { /* … */ } $foo = foo(…); $foo(a: 1, b: 2);
  • Full support for intersection types has been added, allowing you to create new types by combining existing ones. Unlike union types, which define collections of two or more types, intersection types require that the set to be populated does not require any of the listed types, but all the specified types. function generateSlug(HasTitle&HasId $post) { return strtolower($post->getTitle()) . $post->getId(); }
  • There is a new type "never" that can be used to inform static analyzers that a function is terminating program execution, for example by throwing an exception or executing the exit function. function dd(mixed $input): never { exit; }
  • A new array_is_list function has been proposed that allows you to determine that the keys in the array are arranged in ascending order of numerical values, starting from 0: $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
  • You can now use the "final" keyword to prevent parent class constants from being overridden. class Foo { final public const X = "foo"; } class Bar extends Foo { public const X = "bar"; > Fatal error: Bar::X cannot override final constant Foo::X }
  • Functions fsync and fdatasync are proposed to force saving changes from the disk cache. $file = fopen("sample.txt", "w"); fwrite($file, "some content"); if (fsync($file)) { echo "File has been successfully persisted to disk."; } fclose($file);
  • Added the ability to use prefixes "0o" and "0O" for octal numbers, in addition to the previously used prefix "0". 016 === 0o16; // true 016 === 0O16; // true
  • It is proposed to selectively restrict the use of $GLOBALS, which will lead to a violation of backward compatibility, but will make it possible to significantly speed up operations with arrays. For example, the possibility of prohibiting writing to $GLOBALS and passing $GLOBALS by pointer is being considered. An analysis of 2000 packages showed that only 23 of them would be affected by this change. For example, if the proposal in 8.1 is approved, support for expressions such as: $GLOBALS = []; $GLOBALS += []; $GLOBALS =& $x; $x =& $GLOBALS; unset($GLOBALS); by_ref($GLOBALS);
  • Internal methods should now return the correct type. In PHP 8.1, returning a type that does not match the function declaration will generate a warning, but in PHP 9.0, the warning will be replaced with an error.
  • Work continued on transferring functions from using resources to manipulating objects. Functions finfo_* and imap_* have been transferred to objects.
  • Passing null values ​​as arguments to internal functions marked as non-nullable has been deprecated. In PHP 8.1, using str_contains("string", null) will result in a warning, and in PHP 9, an error.
  • Added support for MurmurHash3 and xxHash hashing algorithms.

Source: opennet.ru

Add a comment