PHP 7 has typing arguments and return, but is optional. this is good or bad?

10

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

Come on:

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

I find the two last mentioned in type induction very useful.

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

At this point, I believe you have made it easier, in terms of not having to do ifs within my function / method and throwing an exception if the argument is an unexpected type. PHP 7 already does this.

But here's the question I want to ask: It's also possible not to tell the type 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 of the language feature (which helps in some cases), but what is the reason for adding type induction to the common PHP types, 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?

This makes me a bit confused.

    
asked by anonymous 11.01.2016 / 14:19

2 answers

8

Type Hinting

What the language is doing is putting as much of type hinting as possible, or an aid to the compiler to check contracts in the use of functions. This helps to detect errors more simply and theoretically earlier - which does not happen with PHP because it is a script language.

This seems to be an attempt by PHP to embrace more complex software, which "requires" static typing. Dynamic typing makes it difficult to scale large projects.

A little opinion

I do not quite understand what they are doing, since the niche where language is strong does not need much of it, and if you need to scale the project, Hack , just to stay in the same syntax example, does the full service. Virtually nobody uses Hack and this shows how these features that help scaling projects are unnecessary for these projects that PHP caters. It's just a shame that people love what they do not need.

Do not get it wrong, I find the static typing really important, a lot, but a lot more than OOP, which people have come to love in PHP for no apparent reason.

Dynamic X Static

Language can not and can never, without losing compatibility with everything , be really static, where the gain is more interesting. It can not in part by the existing legacy and partly because this would profoundly change the semantics. It's good that the language stays that way and gives you the option, even though 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 be. But I understand if it does not exist, after all the essence of the language remains , so it would be difficult to define when you can use one thing or another and dynamic typing is very important for a script language, which should require little ceremony.

It is good to make clear that this does not leave the language more dynamic, quite the contrary, it makes it more rigid (not so much because it is not mandatory). It may eventually help to give you 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 a lot of confusion about 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, amazingly, pure OOP fits best with dynamic typing. But since the languages that made the paradigm "pop" are static, a lot of people think it's the other way around.

Conclusion

In a way the question answers itself. I just confirmed what was asked / affirmed. It is the possible improvement. There will be no gain in performance, interoperability, total robustness, or other gains that static typing gives. And because it is optional, it does not lose anything that the dynamic provides.

It is an interesting improvement to use a lot of places, but not at all. Unlike OOP which in PHP, is to use in a few places.

Anyone who understands OOP knows that dynamic typing gives "for free" polymorphism. Many such famous design patterns are unnecessary in this type of language. This is good! If you take proper care! What is not difficult in scripts .

So I think it's nice to be able to induce type, but for PHP, it's even better if it's optional. And to speak the truth, if it is to have static typing even, it is better to use another language. This, despite being welcome, is a misrepresentation of what made language be the success that it is. If people use it well it will be a useful tool. I hope they do not do as OOP which is used where it should not and does nothing useful by the script code.

11.01.2016 / 15:56
5

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

  • Backward Compatibility : Imagine if the new language version required typing in the functions, any php code from an earlier version would be < If you want to upgrade your server to look for other new features, security enhancements, or performance gains, you would be required to update all of your code, which often makes the process unfeasible . See that history 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 already lasts almost 8 years and is still far from over. / li>
  • Keep dynamic language: PHP is by source an untyped script language, this is what attracts a lot of users, so changing the share and enforcing typing would not be "natural."
  • Is typing something good or bad? 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, prototype an idea, however for production code the lack of typing generates uncertainty , you have functions that are given parameters of certain types and return certain types and this is defined by its internal code, it is not enforced by the language, which gives the margin for numerous errors, things like you are expecting a function to return an integer and in fact it is returning a double. In practice working with multiple people in the same code, especially if it is large, this type of problem occurs all the time.

    Just to reinforce my comment about typing "is good": you can see how the programming community is slowly migrating towards typing rather than removal. Popular languages like php and python are adopting optional typing, others like #

    11.01.2016 / 15:45