[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

The savior of monoheritage languages? ? Explaining PHP traits!

PHP

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/

If you found this article helpful , please give it a like!
1
Loading...
1 vote, average: 1.00 / 11
10,562
X facebook Hatena Bookmark pocket
[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

The person who wrote this article

About the author

Yuki Yamada

Joined Beyond Co., Ltd. in 2021
My hobbies are karaoke and board
games.My board games can no longer fit on one shelf, so I would like to buy a new shelf, but I am sad because I don't have a place to put a shelf at home.
He has experience in mold design and sales, and has gained a wide variety of experience and has settled into a job as a server-side engineer.
He is currently working on server-side development using PHP and Python.
He also wants to learn a front-end language in the future, and is interested in Next.js, a React-based front-end framework.