A savior for single-inheritance languages? PHP traits explained!

PHP

Thank you for your hard work!
This is Yamada from the System Development Department.

This time, we will explain traits, which were introduced in PHP 5.4.0 and are used to reuse code

What is a trait?

Traits were introduced in PHP 5.4.0 and are a mechanism for reusing code in single-inheritance languages.
By grouping a group of methods into one and declaring their use within a class, they can be used as class methods without inheritance.

Why traits were created

PHP classes have a mechanism called inheritance
, which is a mechanism for creating a new class while inheriting the contents of an existing class.

Inheritance eliminates the need to write the same code, but this inheritance is limited to one inheritance per class, and if you want to use several methods in various classes, the inheritance relationships can become complicated, making it a little difficult to handle

Traits were born as a way to address this issue.
By grouping methods into traits and making them available simply by declaring their use within a class, the complexity that arises with single inheritance can be reduced.

How to use

How to create a trait

trait trait_name { public function method_name1() { processing } public function method_name2() { processing } }

How to use it in a class

class class name { use created trait name; // class processing continues }

Check the actual code

Let's actually write the code and check if it works.
The things to check are as follows.

  • Do your class methods work properly?
  • Do the inherited class methods work properly?
  • Does it work properly when you declare state in a class?

To achieve this, I would like to create a strong hero, feeling like a god.
When creating a human, I will only define the name and height, and in addition to human characteristics, the hero will be able to wield a sword.
I would also like to create a strong hero by adding physical strengthening characteristics such as "faster legs" and "stronger body."
The code flow is as follows:

  • Create a physical strengthening state
  • Create a hero class that inherits the human class
  • Inherit the created state to the hero class and check if it actually works
 
trait EnhancedBody { public function fastRunner() { echo 'Runs faster'; } public function strongBody() { echo 'Body is stronger'; } } class Human { public $name; public $height; public function __construct($name) { $this->name = $name; $this->height = rand(140, 190); } public function status() { echo "Name: {$this->name}, Height: {$this->;height}"; } } class Swordsman extends Human { use EnhancedBody; public function swingTheSword() { echo "Swings the sword"; } } 

The results of running the above code are shown below

// Create a hero class $taro = new Swordsman('taro'); // Execute the methods defined in the human class $taro->status(); // Name: taro, height: 150 // Execute the methods defined in the hero class $taro->swingTheSword(); // Swing the sword // Execute the methods listed in the body strengthening state $taro->fastRunner(); // Makes your feet faster $taro->strongBody(); // Makes your body stronger

As we have been able to create a stronger hero, we can see that we can use class methods, inherited class methods, and declared state methods

summary

This time, we've explained traits, a mechanism for reusing code in single-inheritance languages.
While the situation in which it can be used is limited, you can use it simply by writing a method in a trait and declaring use in the class you want to use, which reduces code complexity and makes it easier to read, so we encourage you to give it a try!

lastly

I have launched a system development service site called "SEKARAKU Lab," which I work for.
Beyond can handle everything from server design and construction to operation, so if you have any problems with server-side development, please feel free to contact us.
SEKARAKU Lab: https://sekarakulab.beyondjapan.com/

If you found this article useful, please click [Like]!
1
Loading...
1 vote, average: 1.00 / 11
11,490
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Yuki Yamada

Joined Beyond Inc. in 2021.
His hobbies are karaoke and board
games. He wants to buy a new shelf because he can no longer fit all his board games on one, but is sad that he has no space at home for a shelf.
He has also had experience in mold design and sales, and has gained a wide range of experience, settling into a job as a server-side engineer. He is
currently engaged in server-side development using PHP and Python.
He would like to learn a front-end language in the future, and is interested in Next.js, a React-based front-end framework.