Technology world is moving forward and the same holds for PHP too. PHP 8.0 brought many new features, performance improvements and changes such as the new JIT compiler. Now, PHP 8.1 will be released on November 25, 2021 with some exciting features. Let’s see the amazing new features of PHP 8.1.
PHP 8.1 is adding support for enums. They’re user-defined data type consisting of a set of possible values. One of the most common example in programming language is boolean type with true and false as possible values. According to the RFC, enums in PHP will be restricted to “unit enumerations”. According to the PHP team’s survey, it has been found that you can categorize enumerations into three categories- Fancy Constants, fancy Objects and full Algebraic Data Types (ADTs).
PHP implements “Fancy Objects” enums so as to extend it to full ADTs in the future. Conceptually and semantically it is modeled after enumerated types in Swift, Rust and Kotlin, though it’s not modeled on any of them. RFC makes use of famous analogy of suits to explain how it’ll work:
enum Suit { case Hearts; case Diamonds; case Clubs; case Spades; }
Here, enum defines four possible values: Hearts, Diamonds, Clubs, and Spades. One can access those values using syntax: Suit::Hearts, Suit::Diamonds, Suit::Clubs, and Suit::Spades.
As enums are built atop classes and objects, this usage may seem familiar. They have almost the same requirements and behave similarly. Enums share the same namespaces as interfaces, traits and classes. Also, you can define Backed Enums if you want to give a scalar equivalent value to any cases. But, backed enums can only have only one type, either int or string (never both).
enum Suit: string { case Hearts = 'H'; case Diamonds = 'D'; case Clubs = 'C'; case Spades = 'S'; }
Besides, all different cases of backend enum must have a unique value. You can never mix pure and backed enums.
Fibres are PHP’s way of handling parallelism through virtual threads (or green threads). It tries to eliminate the difference between synchronous and asynchronous code by allowing PHP functions to hinder without influencing the entire call stack. You can use Fibres to develop full-stack, interruptible PHP functions that you can use to implement cooperative multitasking in PHP. As Fibres pause the execution stack, you can rest assured knowing that it won’t impact rest of the code.
To illustrate the Fibres use, its RFC uses the simple example-
$fiber = new Fiber(function (): void { $value = Fiber::suspend('fiber'); echo "Value used to resume fiber: ", $value, "\n"; }); $value = $fiber->start(); echo "Value from fiber suspending: ", $value, "\n"; $fiber->resume('test');
In the above code, you’re creating “fibre” and immediately suspending it with string fibre. The echo statement serves as a visual cue for fibre’s resumption. Retrieve this string value from the call to $fiber->start(). Resume the fibre with string “test”, that is returned from call to Fiber::suspend(). The complete code execution results in an output that reads –
Value from fiber suspending: fiber Value used to resume fiber: test
Most of the PHP programmers will never deal with Fibres directly. Considering the performance benefits, you can expect PHP libraries and frameworks to leverage this new feature.
PHP 8.1 adds new file system functions named as- fsync() and fdatasync(). It’ll seem familiar for those used to Linux functions of the same name because they’re related as implemented for PHP. fsync function is just like PHP’s existing fflush() function, however it differs in one way. fflush flushes the app’s internal buffers to the OS, fsync() goes one step further and also ensures that internal buffers are flushed to physical storage. This ensures a complete and persistent write so that one can retrieve data even after an app or system crash.
Read More