PHP 8.1 gives web developers the gift of enums, fibers, and read-only props • DEVCLASS
Even after a good 26 years and strong competition, PHP is still indispensable for the majority of websites. To ensure that it stays that way and that developers have everything they need to make their web projects future-proof, the PHP development team has just released version 8.1 of the scripting language, which is peppered with highly sought-after functions.
One of the most anticipated additions to the release is certainly enumerations, an enumeration type with a fixed number of possible values ââthat is already available in languages ââlike C and Python. It is about the. declared
enum Keyword that should be followed by its name, so the syntax is very similar to that used for classes and interfaces. Options for declaring strings or ints (= backed enums), extending classes, and implementing interfaces are included and are intended to help developers write code that is more readable.
As of PHP 8.1, the language loosens the restriction to new expressions within initialization expressions and allows their use in parameter default values, attribute arguments, static variable initializers and global constant initializers. The update also includes “world class read-only properties” that enable developers “to expose public read-only properties directly, without fear that class invariants could be broken by external changes.” These can only be initialized once and only from the scope in which they were declared, otherwise the property throws an error exception.
Developers who know that a particular function never returns a value, but instead either throws an exception or terminates a request, can share this knowledge with static analyzers by referring to the function as a new return type
never. If a function requires multiple types of input, this can be enforced by using a so-called cut type.
Programmers who want to find out if the keys of a particular array are all integers with no gaps starting at 0 can use a new one
array_is_list Function for that, and you will be delighted to learn that unpacking string keys in arrays is no longer prohibited.
Other improvements range from one
fsync Implemented in the PHP core, on a method of testing that a constant is final, and introducing fibers into the project. Much like its Ruby equivalent, PHP Fibers can be used to create non-blocking execution contexts, although the PHP version is primarily aimed at authors of libraries and frameworks for writing asynchronous PHP code.
Along with the new additions, there is a small list of deprecations that can be found in the PHP wiki. In particular, passing zero to scalar internal arguments without zero values ââor implicitly converting non-integer-compatible float to int results in a warning.
PHP 8.1 appears just days after the announcement of the creation of a PHP Foundation. The non-profit is supposed to ensure the language continues to develop by securing funds for language contributors, but “the current RFC process will not change and language decisions will always be left to the PHP Internals community”.
Apparently the move had been discussed for some time but was initiated after a key employee decided to leave his full-time PHP role to focus on the LLVM infrastructure project. Constituent members of the foundation are Acquia, Automattic, Craft CMS, JetBrains, Laravel, PrestaShop, Private Packagist, Symfony, Todeways and Zend.