PHP Classes
PHP Classes
elePHPant
Icontem

How to Make Better Reuse of PHP Code using Traits Part 1: Basic Code Reuse

Recommend this page to a friend!
  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Make Better Re...   Post a comment Post a comment   See comments See comments (11)   Trackbacks (0)  

Author:

Posted on:

Categories: PHP Tutorials

When you want to share common methods across multiple unrelated classes, in PHP, traits are the way to go.

They contain the power of an abstract class with the added ability to resolve conflicts which makes them the perfect place to store your favorite methods to quickly use in any project.

Read this article to learn what are traits using simple analogies and how you can use them to make better reuse of your code in multiple classes for very different purposes.




Introduction

What is a Trait?

Using Multiple Traits

Conclusion


Introduction

This article assumes you have a basic understanding of the Object Model used in PHP, including inheritance, visibility, properties, methods and class abstraction. You can review these concepts in the articles Extending PHP Classes and the Object Model and Class Abstraction in PHP.

What is a Trait?

Traits are individual pieces of code that define methods that can be used by different classes to provide additional functionality. This is a feature introduced in PHP 5.4.

Traits are a lot like abstract classes with some differences which allow them to be used by several independent classes at the same time. When I think of Traits, I imagine a set of tools like screwdrivers. If a class wants to use them, all they have to do is ask.

trait screwDrivers {

public $type;

public function slottedDriver() {

$this->type = 'slotted';

}

public function phillipsDriver() {

$this->type = 'phillips';

}

public function starDriver() {

$this->type = 'star';

}

}

class myTools {

use screwDrivers;

}

In the above example, the myTools class is using the trait screwDrivers. myTools, which can represent my personal toolbox, now has a set of screwdrivers in it as implemented by the trait. For the purposes of this article, the implementation is pretty basic since each method simply sets the property $type.

Traits can not be instantiated, so you cannot create an object using a trait just by itself. A class must use the traits to access the its methods. The following code instantiates the class, uses one of the traits methods and outputs the result set in the $type property, which will be slotted in this case.

$tools = new myTools();
$tools->slottedDriver();
echo $tools->type;

Using Multiple Traits

I want more than just screwdrivers in my toolbox, so I will add a set of wrenches as well.

trait screwDrivers {

public $driverType;

public function slottedDriver() {

$this->driverType = 'slotted';

}

public function phillipsDriver() {

$this->driverType = 'phillips';

}

public function starDriver() {

$this->driverType = 'star';

}

}

trait wrenches {

public $wrenchType;

public function openWrench() {

$this->wrenchType = 'open';

}

public function boxWrench() {

$this->wrenchType = 'box';

}

public function allenWrench() {

$this->wrenchType = 'allen';

}

}

class myTools {

use screwDrivers, wrenches;

}

myTools is now using screwDrivers and wrenches. Notice that the $type property has been changed to $driverType and $wrenchType in the traits.

If a class defines a property or includes traits which define properties of the same name, an E_STRICT warning is issued. If these duplicated properties do not have the same values or types, then the script will fail with a fatal error.

To avoid this, name properties in a way to ensure they are unique. A more advanced approach is to use accessor methods, which is beyond the scope of this article.

Conclusion

Traits are a great new feature introduced in PHP 5.4. This part of the article introduced you the basics about how to use one or more traits in one class.

The next part covers more advanced topics about traits like solving conflicts when using traits that define the same properties, changing the visibility of a trait method in the class that uses it, and reusing traits within traits.

If you liked this article or you have a question about traits, post a comment here.




You need to be a registered user or login to post a comment

1,497,907 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:

FacebookGmail
HotmailStackOverflow
GitHubYahoo


Comments:

7. Great intro - James Pittman (2015-08-18 19:38)
looking forward to reading part 2... - 0 replies
Read the whole comment and replies

6. Easy to understand. - Imam Kurniawan (2015-08-16 23:39)
Easy to understand.... - 3 replies
Read the whole comment and replies

5. PHP code reuse - Mariano (2015-08-15 20:45)
Ben is a great techer!... - 0 replies
Read the whole comment and replies

4. What are the benefits? - Hugo Wijdeveld (2015-08-14 00:50)
Understand how, but not why.... - 1 reply
Read the whole comment and replies

3. Traits - fast cars (2015-08-13 02:33)
Good article... To the point clear and concise.... - 0 replies
Read the whole comment and replies

2. I Love your tutorials - rodrigo lozano (2015-08-12 18:03)
I've been reading your last tutorials & i've learned a lot... - 0 replies
Read the whole comment and replies

1. clear explanation - Mark Tetrode (2015-08-12 18:02)
clear explanation... - 0 replies
Read the whole comment and replies



  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Make Better Re...   Post a comment Post a comment   See comments See comments (11)   Trackbacks (0)