Traits
Trait Declaration
Section titled “Trait Declaration”A mechanism for code reuse in single inheritance languages like PHP. Traits allow you to reuse sets of methods across multiple classes.
<?php// Declaring a traittrait Logger { public function log($message) { echo "[" . date('Y-m-d H:i:s') . "] $message" . PHP_EOL; }
public function error($message) { $this->log("ERROR: $message"); }}
trait Timestamp { public function getTimestamp() { return time(); }}
// Using traits in classesclass User { use Logger, Timestamp; // Using multiple traits
public function create($name) { $this->log("User created: $name"); return "User created at: " . $this->getTimestamp(); }}
$user = new User();echo $user->create("John") . PHP_EOL; // Output: [timestamp] User created: JohnUsing Traits
Section titled “Using Traits”The process of including a trait in a class using the use keyword to inherit its methods.
<?phptrait Greeting { public function sayHello() { return "Hello"; }
public function sayGoodbye() { return "Goodbye"; }}
trait Math { public function add($a, $b) { return $a + $b; }}
// Using a single traitclass SimpleGreeting { use Greeting;
public function greet($name) { return $this->sayHello() . ", $name!"; }}
// Using multiple traitsclass Calculator { use Greeting, Math;
public function calculateAndGreet($a, $b, $name) { $sum = $this->add($a, $b); return $this->sayHello() . " $name, sum is $sum"; }}
$greeting = new SimpleGreeting();echo $greeting->greet("John") . PHP_EOL; // Output: Hello, John!
$calc = new Calculator();echo $calc->calculateAndGreet(5, 3, "John") . PHP_EOL; // Output: Hello John, sum is 8Trait Conflicts and Resolution
Section titled “Trait Conflicts and Resolution”Handling situations when multiple traits have methods with the same name using insteadof and as operators.
<?phptrait A { public function sayHello() { return "Hello from A"; }
public function sayGoodbye() { return "Goodbye from A"; }}
trait B { public function sayHello() { return "Hello from B"; }
public function sayGoodbye() { return "Goodbye from B"; }}
class Speaker { use A, B { // Insteadof resolves conflicts A::sayHello insteadof B; B::sayGoodbye insteadof A;
// As creates aliases B::sayHello as sayHelloFromB; A::sayGoodbye as sayGoodbyeFromA; }}
$speaker = new Speaker();echo $speaker->sayHello() . PHP_EOL; // Output: Hello from Aecho $speaker->sayGoodbye() . PHP_EOL; // Output: Goodbye from Becho $speaker->sayHelloFromB() . PHP_EOL; // Output: Hello from Becho $speaker->sayGoodbyeFromA() . PHP_EOL; // Output: Goodbye from ATrait Composition
Section titled “Trait Composition”Combining multiple traits together, including the ability for traits to use other traits.
<?php// Base traitstrait Loggable { public function log($message) { echo "LOG: $message" . PHP_EOL; }}
trait Timestampable { public function getTimestamp() { return date('Y-m-d H:i:s'); }}
// Trait can use other traits (trait composition)trait Auditable { use Loggable, Timestampable;
public function audit($action) { $time = $this->getTimestamp(); $this->log("$action performed at $time"); }}
trait Serializable { public function toArray() { return get_object_vars($this); }}
// Composing multiple traits in a classclass Product { use Auditable, Serializable;
public $name; public $price;
public function __construct($name, $price) { $this->name = $name; $this->price = $price; $this->audit("Product created"); }}
$product = new Product("Laptop", 1000);print_r($product->toArray()); // Output: Array with name and price