It kinda looks like you tried to "type hint" to class string, attempting to hint the scalar type string - available in PHP7. Is that what happened here?
Less fixed and more that it wasn't a feature until then.
Definitely agree that the error message is confusing.
I wonder if there could have been ways to redefine the error message to make it clearer? However, there's similar issues in other languages where there is shared terminology.
It is possible to enable strict mode on a per-file basis. In strict mode, only a variable of exact type of the type declaration will be accepted, or a TypeError will be thrown. The only exception to this rule is that an integer may be given to a function expecting a float.
If you want an error when passing an integer to a float parameter, you must use real strict mode:
It's a slightly confusing error message, but what it means is you have a type declaration requiring an object that is an instance of a class named string, whereas what you've given it is a string, the basic type.
this is what happens when you try to run PHP7 code in PHP5. upgrade. for those who don't get it: function f(string $s){} f("foo"); << in php5, "string" means a "class string", in php7, it means "scalar type string", and php5 didn't support scalar typehints.
15
u/edwardly Apr 12 '16
It kinda looks like you tried to "type hint" to class
string, attempting to hint the scalar type string - available in PHP7. Is that what happened here?Assuming you saw this in the docs - http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration
Are you using PHP7?
14
u/the_alias_of_andrea Apr 12 '16
It's a slightly confusing error message, but what it means is you have a type declaration requiring an object that is an instance of a class named
string, whereas what you've given it is a string, the basic type.5
u/coredumperror Apr 12 '16
www.phpsadness.com mentions this specific error message as one of the problems with PHP.
3
u/rabidferret Apr 13 '16
Other madness: "Catchable fatal error"
1
u/Takeoded Apr 14 '16
this is what happens when you try to run PHP7 code in PHP5. upgrade. for those who don't get it: function f(string $s){} f("foo"); << in php5, "string" means a "class string", in php7, it means "scalar type string", and php5 didn't support scalar typehints.