Web developers

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 keep things 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 scripting language, which is peppered with much requested features.

Surely one of the most anticipated additions of 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 expose read-only public properties directly, without worrying that class invariants might be broken by an 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 without spaces starting at 0 can use a new one. array_is_list function for that, 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 equivalent, PHP Fibers can be used to create non-blocking execution contexts, although the PHP version is mainly intended for authors of libraries and frameworks for writing asynchronous PHP code.

With the new additions comes a small list of depreciations, which can be found in the PHP wiki. Especially, passing zero to non-nullable internal scalar arguments or the implicit conversion of an incompatible float from an integer to int will cause a warning now.

PHP 8.1 comes just a few days after the creation of a PHP foundation announcement. 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.