PHP 8.1 presents web developers with the gift of enums, Fibers, and readonly props

PHP 8.1 presents web developers with the gift of enums, Fibers, and readonly props

Even after a good 26 years around and a strong set of competitors, PHP is still essential to a large portion of websites everywhere. In order to keep it that way and make sure devs have what they need to future-proof their web projects, the PHP development team has just released version 8.1 of the scripting language, which comes studded with highly requested features.

One of the most anticipated additions of the release surely has to be enumerations, an enumerated type with a fixed number of possible values already available in languages like C and Python. It is declared via the enum keyword which should be followed by its name, so the syntax is quite similar to the one used for classes and interfaces. Options to declare strings or ints (=backed enums), extend classes, and implement interfaces are included and are hoped to help devs write more readable code. 

Starting with PHP 8.1, the language relaxes the restriction around new expressions inside initializer expressions and allows their use in parameter default values, attribute arguments, static variable initializers and global constant initializers. The update also contains “first-class readonly properties” allowing developers “to directly expose public readonly properties, without fear that class invariants could be broken through external modification”. Those can be initialised one time only, and only from the scope where it has been declared, otherwise the property will throw an error exception.

Developers who know that a certain function will never return a value, but either throw an exception or terminate a request, can share that knowledge with static analysers by proclaiming the function to be of new return type never. If a function requires its input to be of a number of types, this can be enforced by using a so-called intersection type.

Programmers interested in finding out if the keys of a specific array are all integers without gaps that start from 0 can make use of a new array_is_list function for that, and they will surely be pleased to learn that unpacking string keys into arrays isn’t prohibited anymore. 

Other enhancements range from an fsync implementation in PHP’s core, to a method to test if a constant is final, and the introduction of Fibers into the project. Much like their Ruby equivalent, PHP Fibers can be used to create non-blocking execution contexts, though the PHP version is mainly aimed at authors of libraries and frameworks for writing asynchronous PHP code.

Along with the new additions comes a small list of deprecations, which can be found in the PHP wiki. Notably, passing null to non-nullable scalar internal arguments or implicitly converting non-integer-compatible float to int will cause a warning now. 

PHP 8.1 hits just a couple of days after the formation of a PHP Foundation was announced. The non-profit is meant to make sure the language continues to be developed by securing funds for language contributors, however “the current RFC process will not change, and language decisions will always be left to the PHP Internals community.” 

Apparently, the step had been discussed for a while already, but was kick-started after one of the main contributors decided to move away from his full-time PHP role to focus his efforts on infrastructure project LLVM. Constituting members of the foundation are Acquia, Automattic, Craft CMS, JetBrains, Laravel, PrestaShop, Private Packagist, Symfony, Todeways, and Zend.