具有已定义类型的php 7函数返回与该类型相关的错误

0 php php-7

我的代码有问题:

declare(strict_types=1);

class TextModifications
{
    static public function readValue(string $string): string
    {
        return $purifier->purify($string);
    }

    static public function saveValue(string $string): string
    {
        return $string;
    }
}
$TextModifications = new TextModifications();
$TextModifications->saveValue($_POST["login"])
Run Code Online (Sandbox Code Playgroud)

我有错误: 致命错误:未捕获的TypeError:传递给TextModifications的参数1 :: saveValue()必须是字符串类型,给定null,在第21行的/Applications/XAMPP/xamppfiles/htdocs/1.php中调用并在/Applications/XAMPP/xamppfiles/htdocs/1.php:11堆栈跟踪:#0 /Applications/XAMPP/xamppfiles/htdocs/1.php(21):TextModifications :: saveValue(NULL)#1 {main}抛出/第11行的Applications/XAMPP/xamppfiles/htdocs/1.php

我有PHP 7.2.

有谁知道如何解决这个问题?

小智 5

似乎您将NULL作为第一个参数传递给需要字符串参数的函数.$_POST['login']在调用函数之前检查变量是否为null,或者接受null值.这取决于您想要验证输入数据的位置.