Some time ago I wrote an article about the implementation of type hints for non-object types for PHP. Meanwhile many things happened and that implementation was replaced by a different one. Readers of my previous post might know that I have doubts about type hints in PHP. People who met me in person and asked me about it know for sure 
So what's the status now? - Well type hints, for non-object types, exist and they don't. There is a valid syntax which looks like this:
function foo(int $i) {
echo $i;
}
What's the consequence of this code? - Well the type hint is simply ignored. This means that
foo("Hello world");
Will run without any error and print Hello world. So there is an syntax looking like another part of the language which throws errors but behaves completely different.
function foo(bar $b) { }
foo("bar");'
Catchable fatal error: Argument 1 passed to foo() must be an instance of bar, string given [...]
The int hint is just one of them, there are a few more:
- bool, boolean
- string, binary
- scalar
- numeric
- int, integer, long
- real, double, float
- resource
- object
So why is there a syntax added which is ignored? Well there are two things one can do: The hint can be read via the
tokenizer to do some magic or one might create a PHP extension which hooks into the engine to do stricter checks on this, like for a testing system. Does it make sense? - Well, I let it to you to decide whether it make sense to have two syntaxes which look the same but do very different things (being ignored vs. throwing a terminating error) and whether it makes sense to push a system where the core language behaves differently depending on the system (is an extension using this hook loaded? which of them? - one which casts or one which throws an error?) ... I seriously hoped PHP was out of the era of introducing new inconsistencies ...
Tuesday, November 23. 2010 at 04:54 (Link) (Reply)
Tuesday, November 23. 2010 at 06:53 (Link) (Reply)
The beauty of PHP lies in its way of being 'loosely couple'. You just have to 'know what you are doing'!
Tuesday, November 23. 2010 at 07:50 (Link) (Reply)
I've read few comments on the downsides of using scalar type hints, but I've seen a lot of disagreement about their implementation.
Tuesday, November 23. 2010 at 08:49 (Reply)
http://www.mail-archive.com/php-bugs@lists.php.net/msg92274.html
The current casting implementation is still useless and confusing imo.
Tuesday, November 23. 2010 at 08:58 (Link) (Reply)
Scalar type hinting would be a good thing imho, it should be optional though to keep the loosely typed power of PHP in tact. But when it is used, it should NEVER EVER be ommited. This will only draw confusion and anger among developers within a project.
Tuesday, November 23. 2010 at 09:36 (Link) (Reply)
But the only real use of type hinting is to say - "I'm not gonna check this one - PHP does this for me" - if I still need to do type validation, there's no real point. I mean - if it's just for someone who reads the code, we already have docblock.
PHP has gone into a very large and awesome transition into becoming a high leveled and mature OOP language. This step feels a lot more like something we would find in v4 than in the era of PHP5+
Tuesday, November 23. 2010 at 12:29 (Reply)
Tuesday, November 23. 2010 at 12:57 (Link) (Reply)
IMHO this is a very good step for the maturity of the language. However, this kind of changes could decrease the ease of learning and avert new developers.
Regards,
George
Tuesday, November 23. 2010 at 14:16 (Link) (Reply)
Tuesday, November 23. 2010 at 14:50 (Link) (Reply)
How long will i hear about hinting? good, bad, good, bad ... lets just start using some version and let people decide. I can live without it but would be nice to have an option. From time to time where interface has to be strict any way i would like to just use it not wait another 5 years.
lets just get on with it :- )
cheers
Tuesday, November 23. 2010 at 16:48 (Reply)
Tuesday, November 23. 2010 at 19:21 (Reply)
Wednesday, November 24. 2010 at 04:43 (Reply)
assert(is_string($arg1));
Friday, November 26. 2010 at 21:27 (Reply)
Friday, November 26. 2010 at 21:31 (Reply)
Thursday, November 25. 2010 at 19:55 (Reply)
Friday, November 26. 2010 at 15:41 (Reply)
If I would these, i write
"function foo($i)"
NOT
"function foo(int $i)"
> "but this seems to be the worst possible solution."
No, it's not "seem" - it is the worst possible solution!
Monday, November 29. 2010 at 23:46 (Reply)
I've seen someone extending the behaviour to enforce expected behaviour, but seriously... why don't have it natively. There always can be on/off config
Tuesday, November 30. 2010 at 10:49 (Reply)
It's ok to not use hinting.
But if you use hinting you want it.
So if I wirte
"function foo(int $i)"
and make a function call like that
"foo('Hello world');"
there must be an error or at least a notice.