7
When executing the following code:
function foo(string $text) {
echo $text;
}
foo('bar');
I get the following error:
PHP Catchable fatal error: Argument 1 passed to foo() must be an instance of string, string Given, called in /home/...
I was intrigued because I had never done before with Strings
, I always did with Array
, and it worked.
Question
The error occurred because the method called for an instance of string
, but 'bar'
is not an instance of string
? If 'bar'
is not an instance of string
, what parameter should I put in order for this function to be executed correctly?
I believe that this mistake should be better explained.
OS(en) Question testifying to this answer: http://stackoverflow.com/questions/4103480/really-php-argument-1-passed-to-my-function-must-be-an-instance-of-string-s (There is even a string class suggestion to allow type-hint like this - I believe the previous, last answer)
– Luiz Vieira
I really thought PHP had a string class.
– Calebe Oliveira
Implement a class
String
in PHP is very overhead. As there is no support for operator overload, you will depend on methods to do simple things like concatenation, etc.– Henrique Barcelos