COMPLETE list of changes in PHP 8.1 – Features & Deprecations – Overview, October 2021

COMPLETE list of changes in PHP 8.1 – Features & Deprecations – Overview, October 2021

PHP 8.1: Features & Changes Overview

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.

Read more about Automatic conversion in PHP 8.1 in here.

Multi Classes Types

multitool gadget

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.

Read more about Intersections Types in PHP 8.1 over here.

Localized Date Formats

flags on the sky, usa

Format your date with local date structure to combine different format and pattern for full date via new class ` IntlDatePatternGenerator and methods like: getBestPattern() .

Read more about new IntlDatePatternGenerator class in this link.

New Warning being throw when converting float to int

numbers going down, dark theme

For plain float type of variables or float being marked as string, there will be thrown a PHP Deprecation about potential data loss.

Read more about new non-integer-compatible float to int conversions in this article.

ENUMS are here!

limited selections, enums

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!

Read more about Enumerations and how to use them in correct way in here.

Deprecation when you pass NULL to non-null param

missing tooth, missing argument

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.

Why in trouble? Read this and find out about passing null to non-nullable arguments.

Support of merging string keys via Spread Operator

dragon ball z fusion, goku, vegeta

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!

What’s Spread Operator and how it was extended in PHP 8.1? Read here.

New feature array_is_list()

batman slapping robin meme, saying: you didn't check type

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 array to json to format of [0, 1, 2] instead of object properties.

Read more about array_is_list(array $input): bool over here.

New octal integer notation

hex to decimal equation

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.

Check out what other examples are supported with PHP 8.1 octal notation.

Secured $GLOBALS array

three globes as PHP globals

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.

There is also a change of pass by references in $GLOBALS, but you can read that here.

Default MySQLi Error Mode

database error  and mysql default 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.

More on MySQLi Error Mode could be found in this page.

MySQLi: fetch_column()

database success icon for new mysqli fetch_column method

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.

Learn more about mysqli:fetch_column(int index) over here.

MySQLi: binding multiple values

binded notes, mysql binded 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.

To get more information on binding in execute() visit this link.

New fsync() function

synced team of players

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.

Checkout how fsync() work under the hood and why it could be OS dependent in here.

Extra return type :noreturn

no return types accepted sign

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 .

Read how new return type throw an exception and when to use it safely in this link.

Asynchronous and lightweight thread execution: Fibers

Optic Fibers lights

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.

Read a lot more about opening PHP core into async world with Fibers, new Classes, Reflection and Exceptions in this thread.

Deprecated Serializable interface

abstract clouds - serialization deprecated

Since PHP 7.4 magic methods of __seralize() and __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.

Why Serializable is broken and bad design? Read about that and more in here.

Inherited static variables

static dots and 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.

Still confused? Checkout this article for more description and examples around inherited static vars.

Return types for internal methods

internal house room

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.

Check how this will impact union return types and when will they be available in this article.

Persistent class constants

origami bird, floating like persistent class constant

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.

See how you could use final class constants in here.

New setAccessible() Reflection method

city lights blurred

For any private method ReflectionMethod or class property ReflectionProperty we can now set its accessibility and call it manually without getting any errors.

View examples usages of setAccessible() in Reflection over here.

Default parameters with “new” keyword

abstract gradients - new keywords

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.

To inspect how you could start to use “new” in initializes navigate to this link.

Callable syntax

dots structure of new 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.

Read more about First-class callable syntax over here.

Read-only class properties

barrier gate for readonly 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.

Check what errors will be raised and how we could define class variables inside the constructor without any other extra definition lines over here.

More deprecations

abstract clouds, depracations in php 8.1
  • date_sunrise() and date_sunset()
  • key()current()next()prev(), and reset() on objects
  • mb_check_encoding() without argument
  • get_class()get_parent_class() and get_called_class() without argument
  • FILE_BINARY and FILE_TEXT constants
  • t fopen mode
  • Passing bool for $value argument of IntlCalendar::roll()
  • Accessing static members on traits
  • strptime()
  • strftime() and gmtstrftime()
  • mhash*() function family
  • ctype_*() function family accepts int parameters
  • Return by reference with void type
  • NIL constant defined by the IMAP extension
  • Calling overloaded pgsql functions without the connection argument
  • $num_points parameter of image(open|filled)polygon

Full list of deprecated features can be found in this dedicated article.

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.

Leave a Comment