PHP 8.1 has deprecated passing null
as parameters to a lot of core functions. My main problem is with functions like htmlspecialchars(php)
and trim(php)
, where null
no longer is silently converted to the empty string.
To fix this issue without going thrugh huge amount of code I was trying to rename original built-in functions and replace them with wrappers that cast input from null
to (empty) string.
My main problem with this approach is, that the function rename_function(PECL apd)
no longer works, last update on this is from 20041.
I need some sort of override of built-in functions, to avoid writing null check each time function is called making all my code two times larger.
Only other solution I can think of is to use only my custom functions, but this still require going thru all my code un and third party libraries I have.
In PHP 8.1 when null is passed to build in function, it is no longer silently converted to empty string.
Firstly, two things to bear in mind:
htmlspecialchars($something)
can be replaced with htmlspecialchars($something ?? '')
Next, some options:
?? ''
or fixing a logic bug where you weren't expecting a null anyway.nullable_htmlspecialchars
and do a straight-forward find and replace in your code.nullableoverride\htmlspecialchars
; then in any file where you add use function nullableoverride\htmlspecialchars;
that function will be used instead of the built-in one. This has to be added in each file, though, so you may need a tool to automate adding it.?? ''
to appropriate function calls, so you don't have to edit them all by hand. Unfortunately, there doesn't seem to be a built-in rule for this (yet), so you'd have to learn to write your own.?? ''
to simple cases.