首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >迁移到PHP8.1-如何修复不推荐的将null传递到参数错误-在函数中重命名构建

迁移到PHP8.1-如何修复不推荐的将null传递到参数错误-在函数中重命名构建
EN

Stack Overflow用户
提问于 2022-04-01 13:00:34
回答 7查看 38.2K关注 0票数 23

PHP8.1反对将null作为参数传递给许多函数。主要问题是像"htmlspecialchars“和"trim”这样的函数,其中null不再被静默地转换为空字符串。为了解决这个问题,通过大量的代码,我尝试在函数中重命名原始构建,并用包装器替换它们,这些包装器可以将输入从空字符串修改为空字符串。主要的问题是,函数"rename_function“不再起作用,最后一次更新是在2004年。我需要某种形式的内置函数覆盖,以避免每次调用函数时编写空检查,使我的所有代码x2都更大。我唯一能想到的其他解决方案是只使用我的自定义函数,但这仍然需要遍历我所有的代码un和第三方库。

在PHP8.1中,当传递null以构建函数时,它不再被静默转换为空字符串。

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2022-04-02 16:29:42

第一,要紧记两件事:

  1. PHP8.1不支持这些调用,它不会出错。弃用的目的是给作者预先通知来修复他们的代码,所以您和您使用的库的作者直到PHP9.0出来修复这些代码。所以,不要担心不是所有的事情都是马上就解决的,对库维护人员要有耐心,他们会在自己的时间里做到这一点的。
  2. 在大多数情况下,快速修复方法是使用空合并运算符提供一个默认值,因此您不需要在每次使用时都进行长时间的空检查。例如,可以将htmlspecialchars($something)替换为htmlspecialchars($something ?? '')

接下来,有几个选择:

  • 取决于您有多少种情况,您可能一次只需要手动修复几种情况,或者添加?? '',或者修复一个逻辑错误,而不管怎么说,它都不是您期望的null。
  • 创建像nullable_htmlspecialchars这样的自定义函数,并在代码中进行直接查找和替换。
  • 创建自定义名称空间函数(如nullableoverride\htmlspecialchars);然后在添加use function nullableoverride\htmlspecialchars;的任何文件中,将使用该函数而不是内置函数。但是,这必须添加到每个文件中,因此您可能需要一个工具来自动添加它。
  • 使用校长自动将?? ''添加到适当的函数调用中,这样就不必手工编辑它们了。不幸的是,这方面似乎还没有一个内置的规则,因此您必须学会自己编写。
  • 可能更简单,取决于您的技能,使用正则表达式查找和替换将?? ''添加到简单案例中。
票数 28
EN

Stack Overflow用户

发布于 2022-06-15 09:31:13

对于包含大量页面的现有项目,希望迁移到PHP8+的解决方案:

在我的例子中,大多数问题都与接收空值的"trim“函数有关。在这种情况下,您可以创建一个自定义的"trim“函数,然后在现有代码中替换"custom_trim”函数的"trim“:

代码语言:javascript
运行
复制
public function custom_trim(?string $value)
{
    return trim($value ?? '') ;
} 
票数 2
EN

Stack Overflow用户

发布于 2022-08-20 19:25:07

在等待纠正问题时(可能有很多),可以定义一个自定义错误处理函数来忽略它们。

例如:

代码语言:javascript
运行
复制
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;
    }
);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71707325

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档