Hi, I am currently working on my final project of my undergraduate study and the project is about type safety in PHP. One aspect of the project examines the opinion of the PHP community on this topic. I now need your help. I would be very glad, if you could take 5 – 20 minutes to fill in my online survey: http://www.q-set.co.uk/q-set.php?sCode=PGSKQCJUWZVK I will publish the deliverables of my project under an open source license. Thus you contribute automatically to an open source project if you fill in the form Thanks for your help Robert Stoll
Your language is less than exact, it is hard to know what you are talking about. Externally to users, types matter very little. Internally to developers, there is only one type.
Ok, do you have an example? I would like to improve my skills of asking questions. Sorry, but I do not get what you mean. Internally to PHP core developers?
Okay, I'll elaborate, I guess that wasn't quite enough .... To the PHP user, the concept of type safety does not really exist, it's a loosely typed dynamic language. To developers there's only one type of data that matters, the zvalue_value struct, this is a container type that is itself a member of the zval struct ... managed with macros and holds all types ( sometimes a useable reference rather than an actual address, like in the case of objects, which are referenced using an id and handlers, the object store is a [thread local in ZTS mode] dynamically allocated array ). typedef union _zvalue_value { long lval; /* long value */ double dval; /* double value */ struct { char *val; int len; } str; HashTable *ht; /* hash table value */ zend_object_value obj; } zvalue_value; Code (markup): Because of it's design, this cannot be a container that holds references to many types of data, that would destroy efficiency internally and be comparatively complex to the established method of accepting parameters. Now on to the established method of accepting function arguments, zend_parse_parameters takes a specification string, the number of arguments on the stack, and a va_list of addresses to store the parameters at in their true form [provisions are also made to store in the internal zval form]. This ensures that the executing function received the correct types of data from the user, but those types are so loosely defined that it cannot be said to be any sort of type safety. So I think that answers the question, in short: Types matter, and are managed, very little, that's the nature of a loosely typed dynamic language, for everyone. To the developers of the individual extensions, they are working within the framework of a loosely typed dynamic langauge, so type safety for the types they require is entirely down to them.
I totally agree, PHP as it is known now isn't type-safe at all (ok, it has type-hinting but that is not enough to call it type-safe) and I see, that you are talking about the core of PHP. I hope you have filled in my survey Nevertheless, what I am really interested in, is if you think type safety should be improved or not. PHP has already improved it with type hints. What is your opinion? Should PHP remain as it is today and developers of extensions can improve it if they like or should the PHP core be improved in terms of type safety?
No, it shouldn't be improved, nor should type hinting be actually used in the real world, nor is it required. One of the reasons the parser is so hungry for resources is exactly this sort of thing, there are many things in PHP just because they can be, not because they provide some benefit or functionality or performance enhancement, but because it exists elsewhere. That's daft, to me it doesn't make sense that you should implement type hinting in a loosely typed language, and in a dynamic language you shouldn't implement anything that doesn't absolutely have to be there ...
Ok, that's interesting. I suppose you would then agree, that it would be better to have another language like TSPHP which support type safety (and those who want type safety can use it) and PHP could go back to PHP without type hinting and be faster. I am right?
No, not really, another version of PHP, think about it's internal storage method... all you would make is something worse ...
Just in case you are not familiar with my approach of TSPHP. I would introduce TSPHP on a higher level. TSPHP would support static type safety and would be translated to normal PHP during compilation. TSPHP wouldn't change the core or the language specificationon PHP itself. (However, one who would work with TSPHP would need to write TSPHP instead of PHP) I have never thought about that PHP is slower due to the type hints even thougth this is somehow obvious. So keeping that in mind and knowing, that TSPHP does not change the PHP core it would theoretically be possible, that one who wants type safety use TSPHP and one who doesn't could use PHP. And in addition PHP could go back to PHP without type hints and be faster. Or do you see an error in my reasoning?
PHP isn't compiled, it's interpreted. And again, object type hinting is as far as you can take type safety in a dynamically typed language... There's no other way of looking at it, there just isn't ...
I know that PHP is interpreted. Well, maybe my english is too bad to explain it. I will give it another try. You have to think away from PHP to see my approach. Think of TSPHP as an own programming language using a compiler. But rather to compile TSPHP to machine code it is translated to normal PHP. Do you get what I mean? If so, then you probably see also what I meant in my last post otherwise never mind. You gave me a nice hint about performance issues due to type hinting. Thank you
If you want a type-safe server-side language, they already exist. You don't put a serrated edge on a hammer and use it to cut wood when there's a saw handy.
That may be your opinion, fair enough. And no one is using PHP because Phyton was already a dynamic types programming language
I would have to disagree with most of the above developers when it comes to type hinting in PHP. I think type hinting really helps improve your code usability and correctness. In regards to performance, I would say it depends on your situation. Here is an example of when type hinting can be faster (You can try using xdebug/cachegrind to confirm it is faster): No type hinting: class Test { public function doSum($array = array()) { if (!is_array($array) || empty($array)) { throw new Exception('Invalid argument'); } } } PHP: Type Hinting: class Test2 { public function doSum(array $array = array()) { if (empty($array)) { throw new Exception('Invalid argument'); } } } PHP: You can see that using type hinting is faster than doing an is_array. Personally I think that the second example looks cleaner. I started doing your survey, but had to stop on the third question and wasn't able to get it done, the site also seemed slow to me - takes long to load for some reason.
Ok, thank you anyway. I heard now from several person, living outside of europe, that the site is quite slow (up to 30 seconds to load a page). I perfectly understand if one hasn't the patience to wait so long (I wouldn't have it either probably) Concerning the type hinting. I find type hinting very important, that's also one of the reasons why I came up with my approach for TSPHP. krakjoe is referencing to the lost of performance in the PHP core and I think he is right on this part. Think about PHP with type hinting. I think (I do not know it), there must be somewhere in the core a check for each type hint. Now think about PHP wihtout type hinting at all -> the check for the type hint is no longer necessary (at least not in this way) and therefore it will be faster. Make sense?
The examples you gave were different code, doesn't actually show anything. In the real world type hinting is a hinderance. Reading documentation and documenting your own software can achieve bettter usability and correctness and it doesn't cost you at runtime ...
Heya, Quite a long time ago (almost a year), I asked you guys to fill in a survey about type safety in PHP which I conducted in context of my bachelor thesis. Unfortunately, I did not really have time to publish all results on a website so far but I am happy to announce that you can find the results here now: http://tsphp.tutteli.ch/wiki/display/TSPHP/Results+online+survey+Type+Safety+in+PHP My bachelor thesis serves as a basis of the open source project TSPHP. You can find further information about it on the wiki as well: http://tsphp.tutteli.ch/wiki/display/TSPHP/Overview Please don't hesitate if you have further questions about the survey or the project. Cheers, Robert