Traits in PHP 5.4

Due to the fact that PHP has up to now used only a single inheritance model, reusing code  by reducing duplication of code was sometimes a bit of a problem.

To start explaining what traits in terms of PHP are, a good start is to look at how the PHP documentation defines it:

“Traits is a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.”

Buzz words

We have several of buzz words here. They are:

  • Code reuse
  • Single Inheritance
  • Class hierarchies

To fully understand what the definition of a trait means, it is important that you understand what these individual buzz words mean.

Code reuse

Code Reuse is the practice of reusing old code (or software) to create new code (or software). For example, as an overly simplistic example, you could write a single routine, let’s say, HelloWorld, that may do something as simple as write “Hello World” on the screen. You can then use it in other places within your case by simply calling the function HelloWorld which will then automatically put the phrase “Hello World” on the screen where you call this function or routine. An added benefit of code reuse is that should you need to change the message displayed to “Hello Jupiter” it is as simple as changing it in one place in your code. Of course, code reusing has a more prominent place in object oriented programming, but I suspect you are catching my drift.

Single Inheritance

In its most simplest definition, the term Single Inheritance refers to the fact that a class written in a language that utilizes it, can only inherit from one super class.

Class Hierarchies

Class Hierarchies refers to the classification of classes in relation to one another. There are various classes of hierarchies, which falls outside the scope of this article, but in short, some classes can be used as blueprints for others, or an instance of another.

Trait definition revisited

Now that we can distinguish the buzz words in the PHP documentation definition of a trait, the definition makes more sense. Loosely translated, based on the statements above, this can now be simplified as follows:

“A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse functions or routines of code previously developed freely in several independent classes living in different classes that are not related to one another.”

Conclusion

This post is not intended as a “how-to” for using traits. If you’re interested in such a post, you should take a look at one of the following articles, which describes the process clearly: http://phpmaster.com/using-traits-in-php-5-4/ or http://simas.posterous.com/new-to-php-54-traits. Both of these articles explains HOW to use traits. The intention of this article is to explain what it is, define the concept and have a discussion after this. Feel free to post your comments by replying to this post.

 

Leave a Reply

Your email address will not be published. Required fields are marked *