Join Drew Falkman for an in-depth discussion in this video Introducing Horizontal Reuse, part of PHP 5.4 New Features.
Object-Oriented Languages provide us with the ultimate resource for reusing code. One of the main advantages is the concept of inheritance. Through inheritance we can create parent classes that contain core functionality, and then any new class that extends that class will inherit all of its features, its properties, and its methods. However, in PHP you can only extend a single class which means if you want to use functionality from an additional class, you pretty much are left with copying and pasting or using includes or something else like that.
PHP 5.4 introduces the concept of traits. These traits allow us to share functionality from multiple classes without having to extend them. This concept is called Horizontal Inheritance. We have a ClothingProduct class that we want to create to store information about clothing. At its core it has to extend everything from our core Product class, which will have a set of properties like name and price. It might be able to calculate shipping, or return the quantity available and things like that.
In addition we want to make this object usable in our shopping cart system. So we might want to extend the CartItem class as well. Previously, in versions of PHP, we wouldn't be able to do this. We would essentially have to extend one of the classes. You could, maybe have the CartItem class extend the Product Class, and so on. But that creates a chain of inheritance that you may not want in your code. PHP 5.4 introduces a new solution. You can always extend the class as before. So the ClothingProduct being a product wants to have all those properties and methods of the product class.
We also want to have additional traits available to it. For example, we want to be able to add functionality that allows the user to interact with our shopping cart system that's inherent in the CartItem class. The way that we do this in PHP 5.4 is we use this Trait. So the ClothingProduct will extend to the Product Class, but then it'll use the CartItem Trait. In this manner, we get to reuse all that functionality in the CartItem without having to rewrite it or copy paste it or include it which can add to variable collisions and other problems that we may not want.
Additionally, when using Traits we're not limited to a single Trait. We can add as many Traits as we want to a class. So we can extend a single class or have an entire inheritance chain of extensions, and then we can have a number of different Traits that pull in functionality to do different things. The new traits in PHP 5.4 give PHP developers a great way to develop reusable code. Once we've created a Trait we can use all of its functionality and any other class moving forward.
Traits are another reason why PHP 5.4 is a great step to becoming an extremely refined in object-oriented environment.
- Installing PHP 5.4 on Mac and Windows
- Using the built-in web server
- Monitoring upload progress
- Using the high-precision timer
- Using class member access on instantiation
- Introducing horizontal reuse