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

table of contents
Hello!
This is Yamada from the Systems 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. They
allow you to group methods together and declare their use within a class, enabling you to use them as class methods without inheritance.
Why traits were created
PHP classes have a mechanism called inheritance.
Inheritance is a mechanism that allows you to create 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 created as one way to address this problem.
By grouping methods into a trait and making them usable simply by declaring the use of the trait within the class, the complexity that arises from the single inheritance mechanism 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 some code and check if it works.
The things we'll 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 want to create a powerful hero as if I were a god.
The setting is that when creating a human, only the name and height will be defined, but the hero will have the ability to wield a sword in addition to human characteristics.
I also want to add physical enhancement traits such as "faster running speed" and "stronger body" to create a truly powerful hero.
The code will be written 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
In this article, we've discussed traits, one of the mechanisms for reusing code in single-inheritance languages.
While its use is limited to specific situations, it reduces code complexity and makes it more readable because you can simply write a method within a trait and declare it in the class where you want to use it. So please give it a try!
lastly
I have launched "SEKARAKU Lab," a service website for the system development company I belong to.
Beyond offers a one-stop service for everything from server design and construction to operation, so please feel free to contact us if you have any problems with server-side development.
SEKARAKU Lab:https://sekarakulab.beyondjapan.com/
2
