PHP8.1反对将null作为参数传递给许多函数。主要问题是像"htmlspecialchars“和"trim”这样的函数,其中null不再被静默地转换为空字符串。为了解决这个问题,通过大量的代码,我尝试在函数中重命名原始构建,并用包装器替换它们,这些包装器可以将输入从空字符串修改为空字符串。主要的问题是,函数"rename_function“不再起作用,最后一次更新是在2004年。我需要某种形式的内置函数覆盖,以避免每次调用函数时编写空检查,使我的所有代码x2都更大。我唯一能想到的其他解决方案是只使用我的自定义函数,但这仍然需要遍历我所有的代码un和第三方库。
在PHP8.1中,当传递null以构建函数时,它不再被静默转换为空字符串。
发布于 2022-04-02 16:29:42
第一,要紧记两件事:
htmlspecialchars($something)
替换为htmlspecialchars($something ?? '')
接下来,有几个选择:
?? ''
,或者修复一个逻辑错误,而不管怎么说,它都不是您期望的null。nullable_htmlspecialchars
这样的自定义函数,并在代码中进行直接查找和替换。nullableoverride\htmlspecialchars
);然后在添加use function nullableoverride\htmlspecialchars;
的任何文件中,将使用该函数而不是内置函数。但是,这必须添加到每个文件中,因此您可能需要一个工具来自动添加它。?? ''
添加到适当的函数调用中,这样就不必手工编辑它们了。不幸的是,这方面似乎还没有一个内置的规则,因此您必须学会自己编写。?? ''
添加到简单案例中。发布于 2022-06-15 09:31:13
对于包含大量页面的现有项目,希望迁移到PHP8+的解决方案:
在我的例子中,大多数问题都与接收空值的"trim“函数有关。在这种情况下,您可以创建一个自定义的"trim“函数,然后在现有代码中替换"custom_trim”函数的"trim“:
public function custom_trim(?string $value)
{
return trim($value ?? '') ;
}
发布于 2022-08-20 19:25:07
在等待纠正问题时(可能有很多),可以定义一个自定义错误处理函数来忽略它们。
例如:
error_reporting(E_ALL) ;
set_error_handler(
function($severity, $message, $file, $line) {
if ( !$severity || error_reporting()!=E_ALL ) return ; // to treat @ before functions
$erreurs_autorisees = array(
E_NOTICE => "Notice",
E_USER_NOTICE => "User Notice",
E_DEPRECATED => "Deprecated",
E_USER_DEPRECATED => "User Deprecated",
E_WARNING => "Warning",
E_USER_WARNING => "User Warning",
) ;
if ( isset($erreurs_autorisees[$severity]) ) {
$warning_autorises = [
"addslashes(): Passing null to parameter #1 (\$string) of type string is deprecated",
"base64_decode(): Passing null to parameter #1 (\$string) of type string is deprecated",
"htmlspecialchars(): Passing null to parameter #1 (\$string) of type string is deprecated",
"mb_decode_mimeheader(): Passing null to parameter #1 (\$string) of type string is deprecated",
"mysqli_real_escape_string(): Passing null to parameter #2 (\$string) of type string is deprecated",
"preg_replace(): Passing null to parameter #3 (\$subject) of type array|string is deprecated",
"preg_split(): Passing null to parameter #3 (\$limit) of type int is deprecated",
"rawurlencode(): Passing null to parameter #1 (\$string) of type string is deprecated",
"setcookie(): Passing null to parameter #2 (\$value) of type string is deprecated",
"str_starts_with(): Passing null to parameter #1 (\$haystack) of type string is deprecated",
"strcmp(): Passing null to parameter #1 (\$string1) of type string is deprecated",
"strlen(): Passing null to parameter #1 (\$string) of type string is deprecated",
"strtr(): Passing null to parameter #1 (\$string) of type string is deprecated",
"strpos(): Passing null to parameter #1 (\$haystack) of type string is deprecated",
"substr(): Passing null to parameter #1 (\$string) of type string is deprecated",
"trim(): Passing null to parameter #1 (\$string) of type string is deprecated",
"strncasecmp(): Passing null to parameter #1 (\$string1) of type string is deprecated",
] ;
if ( in_array($message, $warning_autorises) ) return true ;
// On ne converti pas les warning en Exception, on se contente de les logger / les afficher
$msg = $erreurs_autorisees[$severity].": $message in $file on line $line" ;
if ( ini_get('display_errors') ) echo $msg ;
// @error_log($msg) ; // if you want to log
}
else throw new ErrorException($message, 0, $severity, $file, $line) ;
return true;
}
);
https://stackoverflow.com/questions/71707325
复制相似问题