Tags

, , , , , ,

The object-oriented programming is all about effective planning and design and interface plays the role of a blueprint; traits help objects to implement that blueprint. Going overboard with traits break that effectiveness.
Let us try to understand that point in the light of our selling application. Consider this code.

//iDeliveryParty.php
namespace MyInterface;

interface iDeliveryParty {
function get($product);
function post($review);
}

However, not every user may want to post a review. In that case, the traits will come to our help for not only implementing the interface but also enhance the re-usability of the code.

//TraitWritingReview.php
namespace MyTrait\traits;

trait TraitWritingReview
{
protected $review = [];

public function post($review)
{
$this->write($review);
}

private function write($review)
{
// express your opinion about the product
$this->review = $review;
return $this->review;
}
}

Now every user can use that trait; however, not every user is in a mood to write a review. In our real world example, the user two gets the product and writes a review.

//UserOne.php
namespace MyClasses;

use MyTrait\traits\TraitWritingReview as TraitWritingReview;

class UserOne
{
use TraitWritingReview;

function get($product) {
echo "The {$product} is delivered to user one.";
}
}
//UserTwo.php
namespace MyClasses;

use MyInterface\iDeliveryParty as iDeliveryParty;
use MyTrait\traits\TraitWritingReview as TraitWritingReview;

class UserTwo implements iDeliveryParty
{
use TraitWritingReview;

function get($product) {
echo "The {$product} is delivered to user two \n";
}

function post($review) {
echo "{$review}";
}
}

Finally, we come to the entry point of our application – “index.php”.

//index.php
use MyClasses\SellingApp as SellingApp;
//use MyClasses\UserOne as UserOne;
use MyClasses\UserTwo as UserTwo;
$sell = new SellingApp();
$userTwo = new UserTwo;
$sell->sell($userTwo);
$userTwo->post("This potato is awesome!\n");

//output
ss@ss-H81M-S1:/var/www/html/mutudu$ php index.php
The Potato is delivered to user two
This potato is awesome!

This example actually helps us to understand the all-important conceptions of “horizontal reuse of code”, in the same breath.
To make the code more efficient, programmers very heavily depend on re-usability of code; and it makes sense in a large application. To do that, they are ready to explicitly handle the conflicts arising from overlapping interfaces. Sometimes this approach stands to benefit from these handling and sometimes it does not. When you use traits and directly motivate classes to implement interfaces, it actually spreads its wings in a horizontal manner. Exactly what happens on the above code example.
If you are ready to accept that code re-usability is one of the greatest motivation behind the object-oriented programming, then you have to make way for an inheritance; for single inheritance in PHP7, code re-usability and clean class hierarchies are always at loggerheads over the maintainability issue. PHP7 comes up with single inheritance and multiple interfaces design patterns, and any single inheritance language has some limitations for code re-usability.
Traits allow for a re-usability of code as they are groups of methods or units of behavioral reuse; another advantage – it is stateless and allows flexible composition of behavior into classes independent of class hierarchies, living in different class hierarchies. This enhances the horizontal reuse or composition of behavior. They are just used to group methods and reuse code being entities made for the classes built for them.

Advertisements