πŸ“œ ⬆️ ⬇️

Types in PHP: good or bad?

Types in PHP: good or bad?

In March 2012, the release of PHP 5.4 was announced. In this version there should have been a lot of interesting innovations, so the developers were waiting for it with impatience. Perhaps the most expected feature of the steel types (traits). Even before the release, Shameer C wrote a wonderful overview of the features that give us the types in PHP. I strongly recommend that you read this article, since my article assumes that you have basic knowledge of types and an understanding of their work.

The PHP community of developers has taken on the types, mainly because they have already been implemented in other popular programming languages, such as Java, C ++ and Python. In addition, the advantages of types were well advertised by the developers, who inserted their five kopecks, on how types will help improve any project, especially as a replacement for inheritance. But are the characters really good? Will they help raise the level of PHP developers, or is this just another fad?

')

Types in PHP is bad


Adding types was mainly motivated by the fact that using them would help reduce code duplication. Also, they can simplify the support of the code, make it cleaner.

Most developers, of course, welcomed this opportunity, but many leading developers feared that the types would be used for other purposes. One such developer is Anthony Ferrara, whose fears have evolved so much that he proclaimed the characters as the next most abused language feature, along with eval and constants. But before Anthony drew such harsh conclusions, he made a very interesting argument: types are in fact a collection of impurities that have practically no state. Implementing types in PHP allows them to have states, so in fact types in PHP are impurities. From this simple fact, the question arises about the true purpose of the introduction of types in PHP. There are no explanations for why types are treated as ordinary impurities, and not as impurities without states, as they ultimately should be.

Anthony continues, stating that the types are similar in functionality to what allows us to make the extends keyword to connect the two classes together. Since this language feature is well known and has been used for a long time, the question arises: is there really a place in PHP or is it an attempt to jump higher than your head?

There is also a question about interfaces. Many developers have only a vague idea of ​​the differences between types and interfaces; You can reuse both types and interfaces, just as with intfefaces, you can inherit types from one another. Are the types something new to PHP, or are they just updated interfaces?

Types in PHP is good


Despite the issues raised, types are good for PHP. They allow us to implement multiple inheritance (as you know, the extends keyword allows only single inheritance to be implemented).

Single inheritance has been used for many years, and it has become an integral part of object-oriented programming in PHP. It limited developers to create complex structures if they wanted to keep the code clean and not redundant. In other languages, multiple inheritance can be used to reduce code duplication in such situations. But in PHP there is no multiple inheritance. Interfaces have been proposed instead, but they are not intended for such use.

Interfaces must act as a contract binding any class to implement the necessary functionality. They can help provide connectivity between classes, but interfaces are not a substitute for multiple inheritance.

Several developers tried to create solutions for multiple inheritance in PHP, but most of these attempts are too redundant, and more like experiments than a real solution.

With the help of types, on the contrary, multiple inheritance can be implemented. As Shameer showed in his article on using types, you can use several types inside the class. John Squib, in his example of using the types of 2011, used several types to implement multiple inheritance.

Most programmers consider multiple inheritance to be evil, pointing to the β€œ diamond problem, ” and claim that single inheritance is less of a headache. But the point is not that it is impractical to use multiple inheritance in PHP, but that such a possibility exists. In light of the many public statements against PHP, this becomes especially important. As a confirmation, simply google "Do not use PHP." With the advent of the use of multiple inheritance, PHP has become a more defiant, expensive and acceptable programming language, and not just the "most common WEB language." I find this quite a comforting fact when developers using C ++, Java or Python try to downplay PHP due to the weak language support of standard programming methods.

Types are quite vivid evidence that PHP is slowly but surely becoming a language that will embrace more and more standard programming techniques, which I hope will attract even more good developers.

Conclusion


The types allow PHP developers to create cleaner, simpler and more efficient code, and also allow them to create more complex systems and experiment with them.

They are not good, they are wonderful! They open up a new level of OOP development techniques for PHP developers, and I believe that this is an omen of the future of PHP.

If you want to familiarize yourself with the topics mentioned in this article, including to deepen your knowledge about types, I suggest the following list of articles:



From the translator: please send remarks on the translation to the PM.

Source: https://habr.com/ru/post/167153/


All Articles