Home  >  Article  >  Backend Development  >  Detailed explanation of trait usage in PHP

Detailed explanation of trait usage in PHP

巴扎黑
巴扎黑Original
2017-08-13 14:09:091452browse

This article mainly introduces how to use traits in PHP. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor and take a look.

To put it simply, the trait keyword is used in PHP to solve the problem that a class wants to integrate the attributes and methods of a base class, but also wants to have methods of other base classes. Traits are generally used in conjunction with use.


<?php
  trait Drive {
    public $carName = &#39;trait&#39;;
    public function driving() {
      echo "driving {$this->carName}\n";
    }
  }
  class Person {
    public function eat() {
      echo "eat\n";
    }
  }
  class Student extends Person {
    use Drive;
    public function study() {
      echo "study\n";
    }
  }
  $student = new Student();
  $student->study();
  $student->eat();
  $student->driving();

?>

The output results are as follows:


study
eat
driving trait

In the above example, the Student class inherits Person and has eat Method, by combining Drive, we have the driving method and the attribute carName.

If there is a property or method with the same name in Trait, base class and this class, which one will be retained in the end?


<?php 
  trait Drive {
    public function hello() {
      echo "hello drive\n";
    }
    public function driving() {
      echo "driving from drive\n";
    }
  }
  class Person {
    public function hello() {
      echo "hello person\n";
    }
    public function driving() {
      echo "driving from person\n";
    }
  }
  class Student extends Person {
    use Drive;
    public function hello() {
      echo "hello student\n";
    }
  }
  $student = new Student();
  $student->hello();
  $student->driving();
?>

The output results are as follows:


hello student
driving from drive

Therefore, it is concluded that when a method or property has the same name, the current class The methods in will override the trait's methods, which in turn override the methods in the base class.

If you want to combine multiple Traits, separate the Trait names by commas:


use Trait1, Trait2;

What happens if multiple Traits contain methods or properties with the same name? ? The answer is that when multiple combined Traits contain properties or methods with the same name, they need to be explicitly declared to resolve conflicts, otherwise a fatal error will occur.


<?php
trait Trait1 {
  public function hello() {
    echo "Trait1::hello\n";
  }
  public function hi() {
    echo "Trait1::hi\n";
  }
}
trait Trait2 {
  public function hello() {
    echo "Trait2::hello\n";
  }
  public function hi() {
    echo "Trait2::hi\n";
  }
}
class Class1 {
  use Trait1, Trait2;
}
?>

The output result is as follows:

Copy code The code is as follows:


PHP Fatal error: Trait method hello has not been applied, because there are collisions with other trait methods on Class1 in ~/php54/trait_3.php on line 20

Use insteadof and as operators to resolve conflicts, insteadof is Use one method to replace another, and as is to give the method an alias. Please see the code for specific usage:


<?php
trait Trait1 {
  public function hello() {
    echo "Trait1::hello\n";
  }
  public function hi() {
    echo "Trait1::hi\n";
  }
}
trait Trait2 {
  public function hello() {
    echo "Trait2::hello\n";
  }
  public function hi() {
    echo "Trait2::hi\n";
  }
}
class Class1 {
  use Trait1, Trait2 {
    Trait2::hello insteadof Trait1;
    Trait1::hi insteadof Trait2;
  }
}
class Class2 {
  use Trait1, Trait2 {
    Trait2::hello insteadof Trait1;
    Trait1::hi insteadof Trait2;
    Trait2::hi as hei;
    Trait1::hello as hehe;
  }
}
$Obj1 = new Class1();
$Obj1->hello();
$Obj1->hi();
echo "\n";
$Obj2 = new Class2();
$Obj2->hello();
$Obj2->hi();
$Obj2->hei();
$Obj2->hehe();
?>

The output result is as follows:


Trait2::hello
Trait1::hi

Trait2::hello
Trait1::hi
Trait2::hi
Trait1::hello

as keyword has another use, which is to modify the access control of methods:

Trait can also be combined with Trait. Trait supports abstract methods, static properties and static methods. The test code is as follows:


<?php
trait Hello {
  public function sayHello() {
    echo "Hello\n";
  }
}
trait World {
  use Hello;
  public function sayWorld() {
    echo "World\n";
  }
  abstract public function getWorld();
  public function inc() {
    static $c = 0;
    $c = $c + 1;
    echo "$c\n";
  }
  public static function doSomething() {
    echo "Doing something\n";
  }
}
class HelloWorld {
  use World;
  public function getWorld() {
    return &#39;get World&#39;;
  }
}
$Obj = new HelloWorld();
$Obj->sayHello();
$Obj->sayWorld();
echo $Obj->getWorld() . "\n";
HelloWorld::doSomething();
$Obj->inc();
$Obj->inc();
?>

The output result is as follows:


Hello
World
get World
Doing something
1
2

The above is the detailed content of Detailed explanation of trait usage in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn