PHP 8.1 gives web developers the gift of read-only enumerations, fibers and props • DEVCLASS
Even after 26 good years and a solid set of competitors, PHP is still essential for a large portion of websites around the world. In order to continue this way and to ensure that developers have what they need to make their web projects sustainable, the PHP development team has just released version 8.1 of the scripting language, which comes with some highly requested features.
Surely one of the most anticipated additions to the release 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 must be followed by its name, so the syntax is quite similar to that used for classes and interfaces. Options to declare strings or integers (= saved enumerations), extend classes, and implement interfaces are included and should help developers write more readable code.
Starting with PHP 8.1, the language relaxes the restriction on new expressions in initialization expressions and allows their use in default values of parameters, attribute arguments, static variable initializers, and global constant initializers. . The update also contains “first-class read-only properties” allowing developers to “directly expose public read-only properties, without worrying that class invariants may be broken by external modification.” These can only be initialized once, and only from the scope where they were declared, otherwise the property will throw an error exception.
Developers who know that a certain function will never return a value, but whether they throw an exception or complete a request, can share this knowledge with static parsers by claiming that the function is of a new return type.
never. If a function requires its input to be of more than one type, this can be applied using an intersection type.
Programmers interested in whether the keys of a specific array are all integers with no spaces starting at 0 can use a new one.
array_is_list function for this, and they will surely be happy to learn that unpacking string keys in arrays is no longer prohibited.
Other improvements range from a
fsync the implementation in the core of PHP, to a method to test if a constant is final, and the introduction of Fibers in the project. Much like their Ruby counterpart, PHP Fibers can be used to create non-blocking execution contexts, although the PHP version is primarily intended for authors of libraries and frameworks to write asynchronous PHP code.
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 an incompatible float from an integer to int will now cause a warning.
PHP 8.1 comes just days after the announcement of the creation of a PHP foundation. The nonprofit aims to ensure that the language continues to be developed by securing funding for language contributors, however “the current RFC process will not change and language decisions will still be left to the PHP Internals community. “.
Apparently, the step had already been discussed for some time, but was initiated after one of the major contributors decided to step down from their full-time PHP role to focus their efforts on the LLVM infrastructure project. The constituent members of the foundation are Acquia, Automattic, Craft CMS, JetBrains, Laravel, PrestaShop, Private Packagist, Symfony, Todeways and Zend.