PHP 8.1 Released With Enums, Read-Only Properties and Fibers |
Written by Alex Armstrong | |||
Tuesday, 30 November 2021 | |||
PHP, the server-side web language used for eight out of ten websites using server-side code, has an annual release cycle and the latest update to this venerable workhorse of the web has just been released. Last year's release, see PHP 8.0 Adds Metadata To Classes, was a major release. While only a decimal point upgrade, PHP 8.1 has many new features, which is a remarkable feat for an open source language that has, until the formation of the PHP Foundation last week, been relatively under-supported. Top of the list of new features is Enumerations (Enums) which allow a developer to define a custom type that is limited to one of a discrete number of possible values. The most popular example of enumerations is the built-in boolean type, which is an enumerated type with legal values According to the release notes: Enums appear in many languages with a variety of different features. In PHP, Enums are a special kind of object. The Enum itself is a class, and its possible cases are all single-instance objects of that class. That means Enum cases are valid objects and may be used anywhere an object may be used, including type checks. Next on the list is the The third new feature, Fibers, addresses the problem that PHP code has until recently been synchronous, which can be slow. The introduction of Fibers seeks to eliminate the distinction between synchronous and asynchronous functions by allowing functions to be interruptible without polluting the entire call stack. According to the release notes: Fibers are primitives for implementing lightweight cooperative concurrency. They are a means of creating code blocks that can be paused and resumed like Generators, but from anywhere in the stack. Fibers themselves don't magically provide concurrency, there still needs to be an event loop. However, they allow blocking and non-blocking implementations to share the same API. Fibers allow getting rid of the boilerplate code previously seen with PHP 8.1 also brings first-class callable syntax; intersection types; and the "never" return type for functions that never return but should either exit / throw an exception / not terminate. In addition the release introduces the final modifier for class constants; new fsync and fdatasync functions; a new array_is_list(), and explicit octal numeral notation. There are several performance-related improvements:
As a result the release notes claim a speedup of 23% for the Symfony demo used as a benchmark compared to PHP 8.0 and and 3.5% for WordPress. PHP 8.1 is now available for download. More InformationRelated ArticlesPHP 8.0 Adds Metadata To Classes PHP - Essential But Underappreciated To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.
Comments
or email your comment to: comments@i-programmer.info |
|||
Last Updated ( Tuesday, 30 November 2021 ) |