Paul Krill
Editor at Large

PHP 8.1 brings enums, read-only properties

news
Nov 30, 20212 mins

Update to the popular scripting language for web development also offers enhancements for concurrency and performance.

coder/programmer sits at the center of a clock
Credit: Kevin Ku

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 <a href="https://wiki.php.net/rfc/readonly_properties_v2" rel="nofollow">readonly</a> 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.

Paul Krill

Paul Krill is editor at large at InfoWorld. Paul has been covering computer technology as a news and feature reporter for more than 35 years, including 30 years at InfoWorld. He has specialized in coverage of software development tools and technologies since the 1990s, and he continues to lead InfoWorld’s news coverage of software development platforms including Java and .NET and programming languages including JavaScript, TypeScript, PHP, Python, Ruby, Rust, and Go. Long trusted as a reporter who prioritizes accuracy, integrity, and the best interests of readers, Paul is sought out by technology companies and industry organizations who want to reach InfoWorld’s audience of software developers and other information technology professionals. Paul has won a “Best Technology News Coverage” award from IDG.

More from this author