Error Reproduced
This eror happen amongst many of the SaaS GovCMS websites, where after the after installing the site locally, imported the production database, and login as administrator using pygmy
& ahoy
, the first thing you see is:
|
|
Also refer to the following screenshot:
Cause of the Error
The error message you encountered, DEPRECATED FUNCTION: STR_REPLACE: PASSING NULL TO PARAMETER #3 ($SUBJECT) OF TYPE ARRAY | STRING IS DEPRECATED
, is related to a change in PHP 8.1. This change deprecates passing null
to certain parameters of built-in functions, including the third parameter ($subject
) of the str_replace()
function. Here’s a breakdown of the issue:
- PHP 8.1 Deprecation: In PHP 8.1, passing
null
to parameters that expect a non-nullable type (likearray|string
) is deprecated. Previously, such cases were silently converted to an empty string or handled without error. - Code Issue: The error occurs because the
$subject
parameter being passed tostr_replace()
isnull
. This often happens due to:- A variable not being initialized properly.
- A function (like
preg_replace()
) returningnull
due to an error or invalid input, which is then passed tostr_replace()
.
Resolving the Error
To fix this issue, you need to ensure that the $subject
parameter passed to str_replace()
is never null
. For my instance, I simply need to add a null check before calling str_replace()
, and provide a default value (like an empty string) in case it is nul
.
For example:
|
|
|
|
In your specific case, the error maybe coming from somewhere else, and you’ll need to read the error message carefully (and debug upstream function potentially) to find out exactly where it comes from.
(Not Recommended) Quickly Surpressing the Error
Alternatively, you can also quickly surpress the error by uninstalling the module that holds this erronous code, for the above example, the error message is:
|
|
Hence we can derive it comes from the module Custom Permissions (config_perms)
, and as a result you can surpress the issue by simply uninstalling this module; But if you do this, please keep in mind this change though-out your local development, and make sure you don’t push this as a part of the configuration (config sync) to the git repository.