More on scalar type hints in PHP trunk
source link: http://schlueters.de/blog/archives/148-More-on-scalar-type-hints-in-PHP-trunk.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
More on scalar type hints in PHP trunk
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
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK