Mastering PHP Traits: The Secret Weapon for Efficient Code

Throughout this article, we will discuss some of the basics of using and creating PHP traits. We will also cover some best practices for effectively using traits in your code.

The concept of traits is another important element of PHP OOP.

The use of traits is a method of reusing code in multiple classes without the need to use inheritance.

As one of the key advantages of using traits in a codebase, developers can define a set of methods that can be reused across multiple classes, preventing duplication of code and making the codebase more maintainable.



PHP Traits – What are they?

There is only one type of inheritance supported by PHP: a child class can only inherit from one single parent class.

Then, what do we do if we need a class to inherit multiple behaviors at the same time?

  • This problem can be solved by using PHP traits.

PHP traits are used in order to define methods that are capable of being used in multiple classes at the same time.

An object trait can have a method that can be used in more than one class, and the method can have an access modifier (public, private, or protected) on its methods that can override the class-specific method.

In order to declare a Php trait, you must use the trait keyword:

Syntax

<?php
trait TraitName {
// some code here…
}
?>

There is a keyword use that can be used to use a trait within a class:

Syntax

<?php
class MyClass {
use TraitName;
}
?>

Here is an example of how this might work:

Example: 

<?php trait greeting { public function greet() { echo "Good Morning! "; } } class Welcome { use greeting; } $obj = new Welcome(); $obj->greet(); ?>

Example Explanation

  • We have declare one trait here: greeting.
  • The next step is to create a class called Welcome.
  • This class is based on a trait, and will be able to use all the methods of that trait as well as the methods in the trait itself.
  • You can simply use the greeting trait in other classes if they are required to use the greet() function in their functions.
In this way, code duplication will be reduced since there will no longer be a need to redeclare the same method again and again.

PHP Multiple Traits

Let’s take a look at an example in which multiple traits are used:

Example: 

<?php trait morning { public function mor_greet() { echo "Good Morning! "; } } trait afternoon { public function aft_greet() { echo "Good Afternoon!"; } } class Welcome { use morning; } class Welcome2 { use morning, afternoon; } $object1 = new Welcome(); $object1->mor_greet(); echo " "; $object2 = new Welcome2(); $object2->mor_greet(); $object2->aft_greet(); ?>

Example Explanation

In this code, two traits are defined: “morning” and “afternoon“. The “morning” trait contains a single method “mor_greet()” that echoes the message “Good Morning!“. The “afternoon” trait also contains a single method “aft_greet()” that echoes the message “Good Afternoon!“.

Two classes are defined: “Welcome” and “Welcome2“. The “Welcome” class uses the “morning” trait, while the “Welcome2” class uses both the “morning” and “afternoon” traits.

Two objects are instantiated from the classes, $object1 from the “Welcome” class and $object2 from the “Welcome2” class.

When the “mor_greet()” method is called on $object1, it echoes “Good Morning!” to the screen. When the same method is called on $object2, it also echoes “Good Morning!“.

When the “aft_greet()” method is called on $object2, it echoes “Good Afternoon!” to the screen.

The output of the code will be:

Good Morning!
Good Morning! Good Afternoon!
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *