• Tue. Oct 22nd, 2024

PHP 8.1 brings enums, read-only properties

Byadmin

Nov 30, 2021


PHP 8.1, billed as a major update to the popular scripting language for web development, has been released with capabilities ranging from enums and read-only properties to first-class callable syntax.PHP 8.1 was released on November 25. Source code and Windows binaries can be downloaded from php.net.Enums, or enumerations, allow developers to define a custom type that is limited to a discrete number of possible values. This can be helpful when defining a domain model by “making invalid states unrepresentable,” according to PHP documentation. In PHP, enum cases are valid objects that can be used anywhere an object may be used, including type checks.Other new features and improvements in PHP 8.1:
Fibers provide primitives for implementing lightweight concurrency.
With first-class callable syntax, closures for callables can be created using the syntax myfunc(…), which is identical to Closure::fromCallable(‘myFunc’).
The readonly property modifier prevents modification of a property after initialization.
Performance has been improved with a JIT (just in time) compiler back end for ARM64, inheritance cache, fast-class name resolution, and improvements to the standard library file system iterators.
With initializers, objects now can be used as default parameter values, static variables, and global constants, along with being used in attribute arguments. This effectively makes it possible to use nested attributes.
Intersection types can be used when a value needs to satisfy multiple type constraints simultaneously.
Developers can declare final class constants, so they cannot be overridden in child classes.
It is now possible to write octal numbers with the explicit 0o prefix.
A never return type indicates the function does not return.
Arrays can be unpacked with string keys.
New classes, functions, and interfaces have been introduced, including an attribute for  #[ReturnTypeWillChange].
Passing null to non-variable internal function parameters is deprecated.
PHP 8.1 follows by nearly a year the release of PHP 8.0, which offered features such as union types and just-in-time compilation. Also new to PHP this month was the formation of the PHP Foundation, a non-profit organization intended to ensure the long life and prosperity of PHP, with members such as JetBrains and Zend. Participants will donate to cover salaries for PHP core developers.

Copyright © 2021 IDG Communications, Inc.



Source link