PHP 7 has argument typing and return, but is it optional. Is that good or bad?

Asked

Viewed 995 times

13

I would not like to raise here a controversial issue regarding the PHP 7 language, which was released recently, but only understand a few points regarding typing.

Come on:

In versions prior to PHP 7, the functions have no return typing. We have type induction for the values array, objeto and interfaces.

I find very useful these last two cited in type induction.

However, with the release of PHP 7, support for type induction was added float, int, bool, string and etc...

At this point, I believe I have made it easier, in relation to not having to keep making ifs within my function/method and to make an exception if the argument is an unexpected type. PHP 7 already does this.

But here is the question I want to ask: It is also possible not to inform the kind of argument that such a function will receive.

Example with type induction:

function soma(int $a, int $b) : int
{
    return $a + $b;
}

Example without type induction:

function soma($a, $b)
{
     return $a+$b;
}

See, you can do both in PHP 7.

So, not wanting to be critical about the language feature (which helps in some cases), but what is the reason for adding type induction to the common types of PHP, if after all who will code will choose whether it will use this feature or not?

Is this to maintain compatibility with legacy code? Or is it to make the language dynamic?

It makes me a little confused.

  • 1

    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.

  • 1

    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 of cast as the value came broken to 1.0 (float), so you would have to use something like return (int) $a;, if you have several returns it becomes easier to induce the exit already. I later formulate an answer :D

  • 1

    @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?

  • 4

    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 read print_r and var_dump.

  • 1

    @Wallacemaxters good example!

Show 1 more comment

2 answers

11


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.

Paradigm

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.

  • 5

    The addition of a more rigid typing changes the language itself, it’s funny that the development team would rather add this feature than fix the (some) string functions that don’t work multibyte caractares, looks like they’re sweeping the dirt under the rug.

7

Strictly speaking the real reason for "why is it so?" you would have to ask to the staff who proposed and voted acceptance of these features, however it is not very difficult to deduce the reasons behind the typing being optional:

  1. Compatibility with previous versions: Imagine if the new version of the language required typing in functions, any php code from an earlier version would be broken in the new version and if you want to update your server to it in search of other new features, security improvements or performance gains would be required to update all your code, which often makes the process unviable. Note that the story already shows that releasing a new version of a language that is not compatible with previous versions generates endless discussions and division in the community, see the transition from python2 to python3 that has lasted almost 8 years and is still far from over.
  2. Keep the language dynamic: PHP is by origin an untyped scripting language, that’s what attracts a lot of users, so changing the comparison and enforcing typing would not be "natural".

Is typing a good thing or a bad thing? This is a subjective question that generates numerous discussions and does not yet have an exact answer. In my opinion: Typing is not only good, it is essential; the dynamics of an untyped language is useful when you want to write something fast, prototyping an idea, however for production code the lack of typing generates uncertainty, you have functions that receive parameters of certain types and return certain types and this is defined by its internal code, not enforced by the language, which gives the margin for numerous errors, things like you’re waiting for a function to return a whole and actually it’s returning a double. In practice working with several people in the same code, especially if it is large, this type of problem occurs all the time.

Just to reinforce my comment on how typing "is good": it is possible to notice how the programming community is gradually migrating towards typing and not removing it. Popular languages like php and python are adopting optional typing, others like Haskell are gaining notoriety due (among other reasons) to their strong typing model.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.