PHP

Using traits

We know that extending from classes allow us to inherit code (properties and method implementations), but it has the limitation of extending only from one class each time. To solve the problem of single inheritance, PHP 5.4 introduced a feature called traits. A trait defines methods intended to be used by multiple classes. The structure of a trait is the same as a class, except that it’s declared using the keyword trait instead of class. You import traits into a class definition with the use keyword.

A trait contains a set of methods and properties just like a class, but cannot be instantiated by itself. Instead, the trait is included into a class and the class can then use its methods and properties as if they were declared in the class itself.

Why to use Traits

The PHP language uses a single inheritance model. This model allows a subclass to explicitly inherit from only a single super class. This means you start with a root class that provides a base implementation. You extend the root class to create more specialized classes that inherit their immediate parent’s implementation. This is called an inheritance hierarchy, and this model works well most of the time. However, what do we do if two unrelated PHP classes need to exhibit similar behavior?

How to create a Trait

The structure of a trait is the same as a class, except that it’s declared using the keyword trait instead of class. It is considered a good practice to define only one trait per file, just like class and interface definitions:

<?php
trait Users {
 
}

Example: Creating a Users trait:

<?php
trait Users {
 protected $firstName;
 protected $lastName;
 protected $address;

 public setFirstName($fn) {
  $this->firstName = $fn;
 }
 public setLastName($ln) {
  $this->lastName = $ln;
 }
 public setAddress($addr) {
  $this->address = $addr;
 }
 public getName() {
  return $this->firstName .  $this->lastName;
 }
 public getAddress() {
  return $this->address;
 }
}

How to use a Trait

Using a PHP trait is easy. We import traits into a class with the use keyword (both namespaces and traits are imported with the use keyword). To use above mentioned Users trait, add the code use Users inside a PHP class definition. Here’s an example:

<?php
class BrainBellLibrary {
 use Users;
 //
}

$lib = new BrainBellLibrary();
$lib->setFirstName ('Brain');
$lib->setLastName ('Bell');
echo $lib->getName();
//Prints BrainBell

The trait’s methods behave as if they were directly defined in that class.

A class may use multiple traits by placing them in a comma-separated list. Similarly, a trait may be composed from one or more other traits.

Inheritance and Traits

Trait methods override inherited methods. Likewise, methods defined in the class override methods inserted by a trait.

<?php
trait MyTrait {
 public function sayHello() {
  echo 'Trait says hello'; 
 }
}

class MyParent {
 public function sayHello() {
  echo 'MyParent class says hello'; 
 }
}

class MyClass extends MyParent {
 use MyTrait;
}

class MySecClass extends MyParent {
 use MyTrait
 public function sayHello() {
  echo 'The child class says hello'; 
 }
}

$a = new MyClass();
$a->sayHello(); //Override the MyParent
//Prints: Trait says hello

$b = new MySecClass();
$b->sayHello(); //Override the MyTrait and MyParent
//Prints: The child class says hello
Advertisement:
Advertisement: