Type Hinting
What language is doing is putting as much as possible hinting type, or a help for the compiler to check contracts on the use of functions. This helps to detect errors more simply and theoretically earlier - which does not happen with PHP as it is a language of script.
This seems to be an attempt by PHP to embrace more complex software that "requires" static typing. Dynamic typing makes scale difficult for large projects.
A little opinion
I don’t really understand what they’re doing, since the niche where the language is strong doesn’t really need this much, and if it needs project scale, Hack, just to stay in the example of the same syntax, do the full service. Virtually no one uses Hack and this shows how these features that help scale projects are unnecessary for these that PHP meets. It’s just a shame people fall in love with each other for what’s not necessary. It shows that people choose technologies like religion and not for technical reasons.
Don’t get me wrong, I think static typing is very important, very, much more than OOP, which people have come to love in PHP for no apparent reason.
Dynamic static X
Language cannot and never can be really static without losing compatibility with all existing, where the gain becomes more interesting. It can’t partly by the existing legacy and partly because it would change the semantics in a profound way. It is good that the language continues like this and gives option, although this is not ideal for robustness.
It would be better if the language could enable a mandatory check for those who prefer it. It may even exist now, but I understand if there is not, after all essence of the language remains script, then it would be hard to define when you can wear one or the other and the dynamic typing is very important for a language of script, which should require little ceremony.
It is good to make clear that this does not make the language more dynamic, on the contrary, it makes it more rigid (not so much because it is not mandatory). It may eventually help to give more robustness, which is good.
And it’s more important to say that this has nothing to do with OOP or other paradigms. I see that there is still much confusion of what is paradigm, methodology, principle, foundation, pattern, system, mode, typing, etc. If paradigms can be orthogonal, imagine other concepts.
Typing has nothing to do with any paradigm, although eventually some may fit better with some typing. And interestingly, astonishingly, pure OOP fits better with dynamic typing. But since the languages that made the "burst" paradigm are static many people think it’s the other way around.
Completion
In a way the question answers itself. I only confirmed what was asked/stated. It is the possible improvement, there will be no gain in performance, interoperability, total robustness, or other gains that static typing gives. And by being optional you don’t miss anything that the dynamic provides.
It is an interesting improvement to use a many places, but not at all. Unlike OOP which, in PHP, is to use in a few places.
Anyone who understands of OOP knows that dynamic typing gives polymorphism "for free" (the high processing cost exists). Many of such Patterns design famous are unnecessary in this kind of language. This is good! If you take proper care! What is not difficult in scripts.
So I think it’s good to have the possibility to induce the type, but for PHP it’s better still that it’s optional. And to tell you the truth, if you’re gonna have static typing, you better use another language. This, while welcome, is a misrepresentation of what made language the success it is. If people use it well it will be a useful tool. I hope you do not do as OOP that is used where it should not and does not do anything useful by the code script.
Extra
And after a while I edit here to put that PHP 8.0 decided to practically turn into a static language and determine which PHP wants to be Java. It is very strange and the result is not good. A language is not born of script and becomes Nterprise without a high price to pay.
I think it is the most obvious. Maintain backward compatibility. It is not interesting to lose users or discourage them from migrating to new versions.
– Daniel Omine
Of arguments seems to me very useful, it helps to work better and avoid things like is_string, is_array, is_int, etc. Now on
return
seems even unnecessary in most cases, maybe it’s a matter ofcast
as the value came broken to 1.0 (float), so you would have to use something likereturn (int) $a;
, if you have severalreturns
it becomes easier to induce the exit already. I later formulate an answer :D– Guilherme Nascimento
@Guilhermenascimento I do not know if the question of
return
. It fits me very well in the framework I’m doing. I think it facilitates the understanding of an implementation of an interface, for example.ArrayableInterface::toArray() : array
. Sack?– Wallace Maxters
I know it’s a "oversimplification", but basically where typing will help most is finding bugs and/or inconsistencies earlier. By parsing, PHP will already be able to warn you of a number of unsupported types that you would normally only discover by calling all functions in all possible conditions. Or on (Argh!.) unit tests, if you’re lucky.
– Bacco
@Bacco read
print_r
andvar_dump
.– Wallace Maxters
@Wallacemaxters good example!
– Guilherme Nascimento