The upcoming release of PHP 5.4 will contain a feature that for many will make the decision to upgrade a no-brainer. This feature called Traits is a mechanism for horizontal code reuse that provides a way for a set of methods to be used by multiple classes in independent class hierarchies. Traits gives developers a way to design code blocks that can be plugged into any class at compile time. This mitigates many of the limitations of a single inheritance model.
A developer begins by defining methods in a block preceded by the keyword “trait” and an identifier. Once you’ve defined a trait, you import its methods into a class with the keyword “use” followed by the trait name. You can import more than one trait per “use” statement by including multiple trait names separated by commas. Additionally, you can can limit which methods are imported by defining a block following the trait names. The traits feature includes conflict detection, and syntax for indicating which methods to use when you attempt to use more than one trait with the same method. Here is an example:
trait bigBar { public function toString() { return implode(', ', $this->_data); } public bar() { return 'Big ' . $this->_bar; } } trait littleBar { public bar() { return 'Little ' . $this->_bar; } } class foo { use bigBar, littleBar { bigBar::bar; littleBar::bar as smallBar; } }
The first line inside the “use” block resolves the conflict between the bar methods present in both traits. The second line renames littleBar’s “bar” method to smallBar. This makes it possible to use both “bar” methods.
I can imagine horizontal code reuse being messy if one were allowed to import methods from one class into another, but the syntax of Traits ensures that developers only use components that are designed for reuse, rather than piecing them together from a variety of sources. I see Traits making code easier to maintain, not only because it simplifies reuse in a number of contexts, but also because it will be easier to test code included at compile time.
This is the second minor release of PHP that has added the kind of language features that are commonly associated with major releases. Version 5.3 introduced both namespaces and lambdas, and with the addition of Traits, PHP has gained three truly groundbreaking features in two minor version updates. It will be interesting to see whether the PHP team continues to add language features with minor releases – or maybe these point releases shouldn’t be thought of as minor releases at all.
You can read more about Traits here:
https://wiki.php.net/rfc/horizontalreuse