Problem z funkcją z zdefiniowanymi typami w php7

0

Witam serdecznie,
Mam taki kod:


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 otrzymuję błąd:
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

Mam PHP 7.2.

Wie ktoś może jak naprawić taki problem?

0

"must be of the type string, null given"

Rozumie się samo przez sie :)
$_POST['login'] == null

0

Jeżeli chcesz dopuścić null'e to dodaj "?" poprzedzające deklarację string'a
public function test(?string $var).
Jeżeli zawsze oczekujesz string'a, to upewnij się, że to co przekazujesz do funkcji rzeczywiście nim jest.

0
  1. Jeśli nie chcesz zmieniać nagłówka funkcji, możesz wywołać:
TextModifications::saveValue((string) $_POST['login']);
  1. W TextModification::readValue() masz błąd - próbujesz wywołać metodę purify na obiekcie, którego nie ma. Zmienna $purifier nie istnieje.

1 użytkowników online, w tym zalogowanych: 0, gości: 1