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.
class
PHP 8.1 – Callable syntax
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 ...$args
.
PHP 8.1 – Default parameters with “new” keyword
Currently we could only assign a default parameter value of null
, 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.