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!
Deprecation of Automatic conversion of false type to array
You won’t be able to initialize array from falsy values any more this include: only
false values and nothing else.
Multi Classes Types
Combine two or more types of classes (or
array) to be required by the class property, local variable or a return type of the function.
Localized Date Formats
Format your date with local date structure to combine different format and pattern for full date via new class `
IntlDatePatternGenerator and methods like:
New Warning being throw when converting float to int
float type of variables or float being marked as string, there will be thrown a PHP Deprecation about potential data loss.
ENUMS are here!
Limit your variables with pre-selected values setup by using new scalar type of
enum and by as cool and fun as JAVA or C# developers!
Deprecation when you pass NULL to non-null param
So you have some random
null values passed over to internal PHP function for non-null params? Well you are in trouble in PHP 8.1 and even more trouble when PHP 9 would pop in.
Support of merging string keys via Spread Operator
Start merging arrays without
array_merge() completely. Spread Operator have now a support of integer types of keys, as well as string types keys. So you can merge your arrays more safe than before. Merge away!
New feature array_is_list()
Added a new check to
array to see if it contains only an int based keys in natural order (from 0 to n`). Useful when we want to ensure the array is consistent (untouched) and when we want to serialize an
json to format of
[0, 1, 2] instead of object properties.
New octal integer notation
Forget about a case of checking
016 == 16 causing a false response, from now on any octal integer will have to be written in literal notation, which is
0o16 . This only applies to integer types, strings behavior are kept intact.
Secured $GLOBALS array
Not much would care about this one, but it’s worth to mention that previously you could override the whole
$GLOBALS variable with your custom data, now you can only modify its keys/aliases.
Default MySQLi Error Mode
The default error mode for
mysqli calls is now changed from silent mode to Exception mode. This doesn’t affect
PDO object since PHP Data Object is already throwing Exception by default.
New function was added to retrieve first or any other column from the resultset to
MySQLi object. This follows the
fetchColumn() method from PDO and behaves in the same manner.
MySQLi: binding multiple values
MySQLi is gettng a feature to bind arrays to
execute($array) method in single call. This would apply to standard
int index based arrays and associative arrays.
New fsync() function
Synchronize new changes to the file without re-opening the same file again.
fsync() would also throw a warning when the file is not a file pointer. The interface is:
fsync(resource $stream): bool.
Extra return type :noreturn
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 .
Asynchronous and lightweight thread execution: Fibers
Fibers allow the creation of full-stack, incorruptible functions that can be used to develop cooperative multitasking in PHP. This feature eliminates the distinction between sync and async functions by allowing them to be incorruptible without breaking the entire call stack.
Deprecated Serializable interface
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.
Inherited static variables
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.
Return types for internal methods
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.
Persistent class constants
It is now possible to declare a class constant as
final to prevent from overriding the constant by the child class. Of course current class constant functionality remains the same.
New setAccessible() Reflection method
For any private method
ReflectionMethod or class property
ReflectionProperty we can now set its accessibility and call it manually without getting any errors.
Default parameters with “new” keyword
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.
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
Read-only class properties
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.
- Passing bool for
- Accessing static members on traits
ctype_*()function family accepts
- Return by reference with void type
- NIL constant defined by the IMAP extension
- Calling overloaded pgsql functions without the connection argument
More about PHP 8.1
You can read more and official information around PHP 8.1 and PHP 8.2 releases in the link below.