PHP 8.1 has deprecated passing null
as a parameter to a lot of core functions. My main problem is with functions like htmlspecialchars
and trim
, where null
is no longer silently converted to the empty string.
To fix this issue without going through a huge amount of code I was trying to rename the 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
(from PECL apd) no longer works; last update on this is from 2004 1.
I need some sort of override of built-in functions, to avoid writing null checks each time a function is called making all my code two times larger.
The only other solution I can think of is to use only my custom functions, but this still require going through all my code and third party libraries I have.
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.