Search code examples
phpphp-7

The php 7 function with defined types returns an error related to the type


I have problem with my code:

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"])

I have error: Fatal error: Uncaught TypeError: Argument 1 passed to TextModifications::saveValue() must be of the type string, null given, called in /Applications/XAMPP/xamppfiles/htdocs/1.php on line 21 and defined in /Applications/XAMPP/xamppfiles/htdocs/1.php:11 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/1.php(21): TextModifications::saveValue(NULL) #1 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/1.php on line 11

I have PHP 7.2.

Does anyone know how to fix this problem?


Solution

  • Seems that you're passing a NULL as the first parameter to the function which requires a string parameter. Check if your variable $_POST['login'] is null before calling the function or accept null values instead. It'll depend on where do you want to validate your input data.