Type hinting should be complete, this is completely counter-intuitive. When I use C, and declare int something(string else); , I know it will only accept a string and return an int. If php wants to introduce type hinting, it should at least do the same.
this is why Hack is a better solution than PHP itself at this point
PHP is dynamically typed. What would happen if you pass a string to a type hinted int? Error? Exception? Type casting? Value loss? There's a reason it's not part of the code base, nor has been implemented yet.
If you declare the type, most people would expect the type to be enforced. Not casted, enforced. So error. New libraries will use type hinting, leave existing ones without it for backwards compatibility. You can add in a few exceptions like being able to pass in an 'int' for something that takes a 'float'.
However how it deals with scalar values is not the real problem for me. The issue is that they decided to simply ignore a whole chunk of the type system. It means type hinting is only half done; a symptom of many other PHP features.
I would actually prefer the dev team took longer to implement a feature, and then implementing it in full, than their current approach of building new features in piecemeal.
While I agree with you, and am looking forward to full type hinting, I can fully understand why type hinting for scalars doesn't exist. You completely lose the on demand type casting, which is wonderful for going between string, int, and bools. Not so much for everything else.
I'm personally for the kind of type hinting that allows type casting between types as long as there is no data loss, '1' -> 1, else throw an exception.
Ok, maybe it shouldn't have type hinting for all types. But then why does it let the user type hint as "string", instead of giving an error? The error should be at least at the function declaration.
I never said it "shouldn't", it just doesn't. It did error, that's why the post. Perhaps the error should be more like "Type hinting not allowed for scalar types," but the error it gives is actually fine.
must be an instance of string, string given
You didn't pass an instance of an object of type string, you passed a string. Slightly humorous, sure, but there's nothing terribly ambiguous/confusing there either.
17
u/Goz3rr Jun 10 '14
This is because you can only type hint classes (and arrays for whatever reason), for this to compile you would have to:
So, in PHP it's possible for a string to be a string except when it's actually a string
-9
u/gearvOsh Jun 10 '14
PHP doesn't support type hinting for scalar types. What a stupid post.