The savior of monoheritage languages? ? Explaining PHP traits!
thank you for your hard work!
My name is Yamada from the System Development Department.
This time, I will explain the traits introduced in PHP 5.4.0 that are used to reuse code.
What is a trait?
Traits were introduced in PHP 5.4.0 and are a mechanism for code reuse in single inheritance languages.
By grouping methods together and declaring their use in a class, you can use them as class methods without inheritance.
Why was trait created?
PHP classes have a mechanism called inheritance.
Inheritance 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 designed to allow only one inheritance per class, so if you want to use several methods in various classes, you can use inheritance. The relationship between them was often complicated, making it a bit difficult to handle.
Trait was born as one way to deal with this problem.
By grouping methods together as traits and allowing them to be used simply by declaring them in a class, we can reduce the complexity that occurs with the single inheritance mechanism.
How to use
How to create a trait
trait trait name { public function method name 1() { processing } public function method name 2() { processing } }
How to use within class
class class name { use created trait name; // class processing thereafter }
Check with actual code
Let's actually write the code and see if it works.
The details to be confirmed are as follows.
- Are your class methods working properly?
- Does the inherited class method work properly?
- Does it work properly when state is declared in a class?
To that end, I would like to create a strong hero who feels like a god.
When creating a human, only the name and height are defined, and the hero has human characteristics and the ability to swing a sword.
We would also like to create a strong hero by adding physical strengthening characteristics such as ``faster feet'' and ``stronger body.''
The flow of writing the code is as follows.
- Create a state of physical strength
- 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 'My legs become faster'; } public function strongBody() { echo 'My body becomes 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 "Swing the sword"; } }
The result of running the above code is shown below.
// Create the hero class $taro = new Swordsman('taro'); // Execute the method defined in the human class $taro->status(); // Name: taro, height: 150 // Hero class Execute the method defined in $taro->swingTheSword(); // Swing the sword // Execute the method defined in the body enhancement state $taro->fastRunner(); // Become faster $taro ->strongBody(); // Makes your body stronger
Since we were able to create a stronger hero than above, we can see that we can use the class method, the inherited class method, and the declared state method.
summary
This time, I have explained traits, which is one of the mechanisms for reusing code in single inheritance languages.
Although the usage itself is a limited function, you can use it simply by writing a method in a trait and declaring a use for the class you want to use, which reduces the complexity of the code and makes the code easier to read, so please use it. Please take a look!
lastly
I have opened the system development service site "SEKARAKU Lab" to which I belong.
Beyond is a one-stop service for everything from server design and construction to operation, so if you have any trouble with server-side development, please feel free to contact us.
SEKARAKU Lab: https://sekarakulab.beyondjapan.com/