Guide to PHP SecurityQuicksearchCalendar
|
Saturday, July 4. 2009Type hinting rehashed (now with type casting support)
There has been a lot of comments both on this blog and the internals list. There seems to be a fairly large group of core developers who like the idea as well as surpassingly large support base on the user level too (wow, didn't think that this many people want type hinting). Unfortunately, there have also been, as is typically on the internals list a few people complaining for the sake of complaining. Their arguments have ranged from type hinting is against PHP's loosely typed nature and people will mis-use it and make PHP into something that it is not, to I don't need or will use it, so no one should get it.
That said these people are in the minority, albeit a rather vocal one, so progress is being made. There have also been a number of really good suggestions by folks who have reviewed the patch (big thanks guys) and their improvements have been incorporated into the version 2 of the patch. Here is the quick changelog. 1) Added support for "object" type hint 2) Modified the patch not to break binary compatibility so you can now use patched PHP without having to re-compile your extensions or having issues with binary ones. 3) Added full reflection support, which I appropriated from Felipe's earlier work on type hinting 4) Added support for type casting, where by you can do things like this function a((int)$foo), which will force PHP to cast the value of $foo to an integer 5) Added tests, which again I partially appropriated from Felipe's earlier work. The updated patch is available here: http://ilia.ws/patch/type_hint_53_v2.txt The mini test suit which can also show practical examples & limitations of the functionality can be found here: http://ia.gd/patch/type_hint_tests.tar.bz2 I am posting it here for peer review and comment. If all goes well, I am going to post it to internals for a vote, which will hopefully pass. So, if you want the feature in, or you think its crap, watch internals on Monday so you can make your opinion known via a +/- vote. Trackbacks
[PHP]Type hinting/casting request for vote
via. Type hinting for PHP 5.3 - iBlog - Ilia Alshanetsky?Type hinting rehashed (now with type casting support) - iBlog - Ilia Alshanetsky ??????1????Ilia????????????????????????????????????? Last week or so
Weblog: Do You PHP ???
Tracked: Jul 07, 20:39 Comments
Display comments as
(Linear | Threaded)
Hi Ilia,
I love your patch, especially the object and scalar type hints, as well as strict type hints for every scalar type. Still, I have some questions about it: - How does the float type hint react on ints? It should be valid to submit an int as a float, without casting it explictly, IMO. - How do type hints interact with casting in the method/function declaration? Is it possible to mix them? - How do casts get reflected? Do they also indicate the casted type through reflection? Thanks a lot for your efforts in this direction! Regards, Toby
If you ask for a float or int and provide another type it'll be rejected.
You can mix type hints in function/method declaration, as well as overload type hints on methods. Casts are being reflected, see the reflection test for casts.
What about arrays? Often is desirable to have a guarantee about the exact type of the array.
phplint provides a convenient syntax for annotating arrays http://www.icosaedro.it/phplint/phpdoc.html#types array - index can be both integers and strings, elements mixed array[] - index can be both integers and strings, elements mixed array[int]string - array of strings with int index array[int][int]float - matrix of floating-point numbers array[string]mixed - associative array of generic values array[]SomeClass - array of objects of the class SomeClass, the keys can be both integer numbers and strings Could this be included in the patch?
Hi Ilia,
+1 for the *possibility* to enforce type I haven't been following on internals so maybe my question has already been debated there. Would it be possible to force types on function parameters the way it is already done with object types, ie: function a(int $foo) ? This is more in line with the syntax style actually being used and better expresses what you're actually trying to achieve: enfore the parameter's type. Thanks, Steven
Now if we could only get function overloading so that I could do something like:
function foo(int bar) { } function foo(string bar) { }
Hi,
first off, great work and good to know that PHP internals still didn't give up on scalar type hinting. Keep it up. I also have two questions if you don't mind. 1. What is the difference to Felipe Pena patch, besides that he also allows return type hinting (something I would love to have as well ... sorry if I am being blunt!) 2. Which path do you use in work? You mentioned that in your first blog post when introducing your new patch.
Felipe's patch and mine were fairly similar at the onset. The difference is that my patch also support type forcing via (int) and similar. Also, the patch Felipe proposed breaks binary compatibility, while mine retains it, which is important if you intend to apply it against PHP where binary modules are used, especially those you cannot recompile.
Hi Again,
I a a bit confused, so sorry for my ignorance. But doesn't Felipes path also support this: integer / int function test(integer $value) { } His RFC states that at least. Anyway, sounds like yours is the way to go ... I am just curious how that's going to come out, and if I should use it my projects. I sure would love to have it, at least in my development system, und remove it for production to at least spare the customers the need for rebuilding after each PHP update. Thanks for your work and efforts, Cheers
Wow, this looks great! Would really love to see this feature in future PHP!
I'm afraid but your patch won't compil :
I've got this error when I try to : Zend/zend_language_scanner.l: In function 'lex_scan': Zend/zend_language_scanner.l:1170: error: 'T_INT_HINT' undeclared (first use in this function) Zend/zend_language_scanner.l:1170: error: (Each undeclared identifier is reported only once Zend/zend_language_scanner.l:1170: error: for each function it appears in.) Zend/zend_language_scanner.l:1166: error: 'T_STRING_HINT' undeclared (first use in this function) Zend/zend_language_scanner.l:1182: error: 'T_NUMERIC_HINT' undeclared (first use in this function) Zend/zend_language_scanner.l:1162: error: 'T_BOOL_HINT' undeclared (first use in this function) Zend/zend_language_scanner.l:1186: error: 'T_SCALAR_HINT' undeclared (first use in this function) Zend/zend_language_scanner.l:1178: error: 'T_RESOURCE_HINT' undeclared (first use in this function) Zend/zend_language_scanner.l:1174: error: 'T_DOUBLE_HINT' undeclared (first use in this function) make: *** [Zend/zend_language_scanner.lo] Erreur 1
Great for PHP 6.0 or maybe PHP 5.4 but it's too late for PHP 5.3.
Great work! I really hope it gets approved and included in the language.
PHP REALLY NEEDS this kind of professional feature to move on and keep evolving as a serious platform. Being "dynamic" is good, but this also leads to all kinds of weird, hard to track bugs. Eduardo
You should never ignore the minority. Just because they're a small group doesn't mean that they won't become majority in the future. And then everyone who ignored them before feels like they missed a huge opportunity.
|
ArchivesCategoriesSyndicate This BlogBlog Administration |
|||||||||||||||||||||||||||||||||||||||||||||||||










Comments