Quantcast
Channel: xlab » programistyczny fail
Viewing all articles
Browse latest Browse all 2

Programistyczny fail: “argument musi być stringiem, przekazano string”

0
0

Dosyć ciekawy błąd przytrafił się koledze piszącemu w PHP:

Catchable fatal error: 
Argument 1 passed to foo() must be an instance of string, string given, called in 
/Users/madmatt/Documents/Projects/foobar/

dosyć mocny WTF. Kod, który to powoduje:

function foo(string $s)
{
}
foo("bar");

Próbujemy tutaj ograniczyć przyjmowane argumenty przez funkcję foo do zmiennych typu string. Co ciekawe, to nie bug, tylko feature (lub też brak tegoż feature). Okazuje się, że php tego nie obsługuje. Powie nam to linijka na samym końcu rozdziału o Type Hinting:

Type Hints can only be of the object and array (since PHP 5.1) type. Traditional type hinting with int and string isn’t supported.

Czyli tablice, klasy – tak, podstawowe typy nie?
Too bad, ale z treścią błędu mogli się bardziej postarać. A to jeszcze inny klasyk z PHP:

Parse error: syntax error, unexpected ')', expecting T_PAAMAYIM_NEKUDOTAYIM in 
Command line code on line 1

To po hebrajsku. Znacie inne tego typu niespodzianki rodem z PHP?


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles