Finally we have a Release Candidate v4 for PHP 8.1. What have changed? and what’s got added new? But mainly what did they decided to drop from the language? Read this and surprise your boss/team with super sweet knowledge of new technologies!
To ensure the class property will never be modified/changed by itself or children or external calls we can now use a
readonly prefix to class property declaration like so:
public readonly string $propertyName = 'thisValueWillNeverChange;. The only place when we could override the readonly variable is inside the class constructor.
Convert any callable into Closure object to be returned and then used/called when needed. The next syntax (aka First class callable syntax) of
$this->methodName(...) is identical in behavior as current:
Closure::fromCallable([this, 'methodName']). Do not confuse this syntax to unpacking argument syntax of
Currently we could only assign a default parameter value of
array or primitive types in functions and methods. In PHP 8.1 we will be able to provide any custom class as default value for our expected function parameter. This includes defining a class variables inside the parameters as well.
Every PHP internal methods from internal Classes will now contain a return type declarations. This change will impact any case of extending and overriding of internal PHP classes with custom implementations. From 8.1 we will get a deprecation and since 9.0 it will be a Fatal Error.
Local method static variables will now be inherited between multiple objects and even classes if base Class is used as parent Class. Previously each local static var was only related to the current type of Class.
Since PHP 7.4 magic methods of
__unserialize() were added, since then Serializable interfece is obsolete and in fact broken with new PHP 8.x features. This also includes deprecating of
PDO::FETCH_SERIALIZE flag of PDO object that will be remove completely in PHP 9.
For any methods or function which are meant to always throw exception or exit the code using things like: exit() or
die() a new return type of:
noreturn was added. The sample usage could be:
function iWillAlwaysExit(): noreturn .