In PHP, traits are like sets of methods that you can reuse in different classes. They're handy when you want to use the same methods in multiple classes without using inheritance (where one class extends another).

  • Define using  trait keyword
  • It have any access modifier (public, private, or protected)
  • To use a trait in a class, you use the use keyword 

Example:

<?php
// Define a trait
trait Logger {
    public function log($message) {
        echo "Logging: " . $message;
    }
}

// Use the trait in a class
class MyClass {
    use Logger;

    public function doSomething() {
        $this->log("Doing something...");
    }
}

// Create an instance of the class and call the method
$obj = new MyClass();
$obj->doSomething(); // Output: Logging: Doing something...

?>

Here, the Logger trait has a log() method. The MyClass uses this trait with the use keyword. Then, we can call the log() method from MyClass, even though it's defined in the trait.