PHP traits: is it good or bad?
- Transfer

In March 2012, the release of PHP 5.4 was announced. In this version, many interesting innovations were to appear, so the developers were looking forward to it. Perhaps the most anticipated feature was traits. Even before the release, Shameer C wrote a great overview of the features that give us traits in PHP. I highly recommend that you read this article, as my article assumes you have basic knowledge of types and an understanding of their work.
The PHP developer community has adopted traits, mainly because they have already been implemented in other popular programming languages, such as Java, C ++, and Python. In addition, the advantages of traits were well publicized by developers who inserted their five cents, as to how traits will help improve any project, especially as a replacement for inheritance. But are traits really so good? Will they help raise the level of PHP developers, or is it just another quirk?
PHP traits are bad
Adding traits was mainly motivated by the fact that their use will help reduce code duplication. They can also simplify code support and 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 developed so much that he has proclaimed the traits of the next most abuseda feature of the language, along with eval and constants. But before Anthony made such harsh conclusions, he made a very interesting argument: types are actually a collection of impurities that have virtually no state. Implementing traits in PHP allows them to have states, so essentially traits in PHP are impurities. From this simple fact, the question arises about the true purpose of introducing traits in PHP. There is no explanation for why types are treated as ordinary impurities, and not as stateless impurities, which they ultimately should be.
Anthony continues, stating that traits are similar in functionality to what allows us to do the extends keywordto link two classes together. Since this feature of the language is well known and has been used for a long time, the question arises: do traits really have a place in PHP or is it an attempt to jump above your head?
The question also arises regarding interfaces. Many developers have only a vague idea of the differences between types and interfaces; you can reuse both traits and interfaces, just like with interfaces, you can inherit traits from each other. Are traits new to PHP, or are they just updated interfaces?
PHP traits are good
Despite the questions raised, traits are good for PHP. They allow us to implement multiple inheritance (as you know, the extends keyword allows you to implement only single inheritance).
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 this application.
Interfaces should act as a contract obliging any class to implement the necessary functionality. They can help provide communication between classes, but interfaces are not a substitute for multiple inheritance.
Several developers have attempted to create multiple inheritance solutions in PHP, but most of these attempts are too redundant, and more like experiments than the real solution.
With traits, by contrast, multiple inheritance can be implemented. As Shameer showed in his article on using traits, you can use multiple traits within a class. John Squib, in his 2011 trait example , used several traits to implement multiple inheritance.
Most programmers see multiple inheritance as evil, pointing to the " diamond problem ""and argue that single inheritance is less of a headache. But the point is not that it’s impractical to use multiple inheritance in PHP, but that it is possible. In light of the many public comments against PHP, this becomes especially important. as a confirmation, just google “Do not use PHP.” With the advent of the possibility of using multiple inheritance, PHP has become a more challenging, expensive and acceptable programming language, and not just the “most common WEB language.” I find this is a pretty comforting fact when developers using C ++, Java, or Python try to downplay PHP because of poor support for standard programming methods.
Traits are a clear indication that PHP is slowly but surely becoming a language that will cover more and more standard programming techniques, which I hope will attract even more good developers.
Conclusion
Traits allow PHP developers to create cleaner, simpler and more efficient code, and also allow you to create more complex systems and experiment with them.
They are not good, they are wonderful! They open a new level in OOP development techniques for PHP developers, and I believe this is an omen of the future of PHP.
If you want to familiarize yourself with the topics mentioned in this article, including deepening your knowledge about types, I propose the following list of articles:
- Ircmaxell's Blog: Are Traits The New Eval?
- John Squibb's Programming Blog: Multiple Inheritance in PHP 5.4 using Traits
- AzonMedia: Multiple Inheritance in PHP - a Workaround
- Refulz PHP: Traits in PHP 5.4 - Why we need Traits
- Christer's Blog o'Fun: Playing with Traits in PHP 5.4alpha1
- Slideshare: PHP Traits, Treat or Threat?
- Refulz PHP: Traits in PHP - Multiple and Nested Traits
From the translator: please send comments on the translation in PM.