What is the use of trait in php?

PHP - What are Traits?

PHP only supports single inheritance: a child class can inherit only from one single parent.

So, what if a class needs to inherit multiple behaviors? OOP traits solve this problem.

Traits are used to declare methods that can be used in multiple classes. Traits can have methods and abstract methods that can be used in multiple classes, and the methods can have any access modifier (public, private, or protected).

Traits are declared with the trait keyword:

Syntax

<?php
trait TraitName {
  // some code...
}
?>

To use a trait in a class, use the use keyword:

Syntax

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

Let's look at an example:

Example

<?php
trait message1 {
public function msg1() {
    echo "OOP is fun! ";
  }
}

class Welcome {
  use message1;
}

$obj = new Welcome();
$obj->msg1();
?>

Try it Yourself »

Example Explained

Here, we declare one trait: message1. Then, we create a class: Welcome. The class uses the trait, and all the methods in the trait will be available in the class.

If other classes need to use the msg1() function, simply use the message1 trait in those classes. This reduces code duplication, because there is no need to redeclare the same method over and over again.

PHP - Using Multiple Traits

Let's look at another example:

Example

<?php
trait message1 {
  public function msg1() {
    echo "OOP is fun! ";
  }
}

trait message2 {
  public function msg2() {
    echo "OOP reduces code duplication!";
  }
}

class Welcome {
  use message1;
}

class Welcome2 {
  use message1, message2;
}

$obj = new Welcome();
$obj->msg1();
echo "<br>";

$obj2 = new Welcome2();
$obj2->msg1();
$obj2->msg2();
?>

Try it Yourself »

Example Explained

Here, we declare two traits: message1 and message2. Then, we create two classes: Welcome and Welcome2. The first class (Welcome) uses the message1 trait, and the second class (Welcome2) uses both message1 and message2 traits (multiple traits are separated by comma).



NDUKWE CHIDERA K.

Sometimes you may want to inherit from more than one class. Since this is not possible in PHP and other single inheritance languages like it, we have something called traits.

To read more about inheritance in PHP, check out this shot.

What is a trait?

In PHP, a trait is a way to enable developers to reuse methods of independent classes that exist in different inheritance hierarchies.

Simply put, traits allow you to create desirable methods in a class setting, using the trait keyword. You can then inherit this class through the use keyword.

Syntax

To create a trait class:

<?php trait TraitName { // some code... } ?>

To access a trait class:

<?php class MyNewClass { use TraitName; } ?

Code

Example 1

Let’s see the code below to understand better.

<?php

class MainClass {

public function greeting() {

echo 'Good Day From MainClass'."\n";

}

}

trait DesiredClass {

public function welcome() {

//parent::greeting();

echo 'Welcome To Traits';

}

}

class NewClass extends Mainclass {

use DesiredClass;

}

$myObject = new NewClass();

$myObject ->greeting();

$myObject->welcome();

/* You can uncomment line 10 while commenting out

*line 20 and the result remains the same.

*/

?>

Explanation

  • A base class was defined as MainClass with a single method.

  • The trait class DesiredClass was also created with another method.

  • The NewClass that was created was able to access the method from both the base class and the trait class which we saw in its object $myobject.

Example 2

<?php

trait hello {

public function message1() {

echo "Nature is precious,\n";

}

}

trait world{

public function message2() {

echo "So Let us unite to preserve it";

}

}

class InUnity {

use hello;

}

class WeCan {

use hello, world;

}

$obj = new InUnity();

$obj->message1();

echo "****************\n";

$obj2 = new WeCan();

$obj2->message1();

$obj2->message2();

?>

Explanation

Multiple traits can be created and used by one or more classes. The code above is an example of multiple trait inheritance.

CONTRIBUTOR

NDUKWE CHIDERA K.

Why do we use traits in PHP?

Traits are used to declare methods that can be used in multiple classes. Traits can have methods and abstract methods that can be used in multiple classes, and the methods can have any access modifier (public, private, or protected).

Why do we need traits?

Traits can define both static members and static methods. It helps developers to reuse methods freely in several independent classes in different class hierarchies. Traits reduces the complexity, and avoids problems associated with multiple inheritance and Mixins.

When can you use traits?

When to Use Traits? If the behavior will not be reused, then make it a concrete class. It is not reusable behavior after all. If it might be reused in multiple, unrelated classes, make it a trait.

What is a trait in programming?

In computer programming, a trait is a concept used in object-oriented programming which represents a set of methods that can be used to extend the functionality of a class.

Chủ đề