PHP的Trait 特性

末蓝、 2023-07-07 12:43 21阅读 0赞

Trait是在PHP5.4中加入的,它既不是接口也不是类。主要是为了解决单继承语言的限制。是PHP多重继承的一种解决方案。例如,需要同时继承两个 Abstract Class, 这将会是件很麻烦的事情,Trait 就是为了解决这个问题。它能被加入到一个或多个已经存在的类中。它声明了类能做什么(表明了其接口特性),同时也包含了具体实现(表明了其类特性)。

简单使用

首先,当然是声明个 Trait,PHP5.4 增加了 trait 关键字

  1. Trait是在PHP5.4中加入的,它既不是接口也不是类。主要是为了解决单继承语言的限制。是PHP多重继承的一种解决方案。例如,需要同时继承两个 Abstract Class 这将会是件很麻烦的事情,Trait 就是为了解决这个问题。它能被加入到一个或多个已经存在的类中。它声明了类能做什么(表明了其接口特性),同时也包含了具体实现(表明了其类特性)。
  2. 简单使用
  3.   首先,当然是声明个 TraitPHP5.4 增加了 trait 关键字

同时,如果要在 Class 中使用该 Trait,那么使用 use 关键字

  1. class first_class {
  2. // 注意这行,声明使用 first_trait
  3. use first_trait;
  4. }
  5. $obj = new first_class();
  6. // Executing the method from trait
  7. $obj->first_method(); // valid
  8. $obj->second_method(); // valid

使用多个 Trait

在同个 Class 中可以使用多个 Trait

  1. trait first_trait
  2. {
  3. function first_method() { echo "method1"; }
  4. }
  5. trait second_trait {
  6. function second_method() { echo "method2"; }
  7. }
  8. class first_class {
  9. // now using more than one trait
  10. use first_trait, second_trait;
  11. }
  12. $obj= new first_class();
  13. // Valid
  14. $obj->first_method(); // Print : method1
  15. // Valid
  16. $obj->second_method(); // Print : method2

Trait 之间的嵌套

同时,Trait 之间也可以相互的嵌套,例如

  1. function first_method() { echo "method1"; }
  2. }
  3. trait second_trait {
  4. use first_trait;
  5. function second_method() { echo "method2"; }
  6. }
  7. class first_class {
  8. // now using
  9. use second_trait;
  10. }
  11. $obj= new first_class();
  12. // Valid
  13. $obj->first_method(); // Print : method1
  14. // Valid
  15. $obj->second_method(); // Print : method2```
  16. ### Trait 的抽象方法(Abstract Method)
  17. 我们可以在 Trait 中声明需要实现的抽象方法,这样能使使用它的 Class 必须实现它
  18. ~~`
  19. trait first_trait {
  20. function first_method() { echo "method1"; }
  21. // 这里可以加入修饰符,说明调用类必须实现它
  22. abstract public function second_method();
  23. }
  24. class first_method {
  25. use first_trait;
  26. function second_method() {
  27. /* Code Here */
  28. }
  29. }

Trait 冲突

  1. ```trait first_trait {
  2. function first_function() {
  3. echo "From First Trait";
  4. }
  5. }
  6. trait second_trait {
  7. // 这里的名称和 first_trait 一样,会有冲突
  8. function first_function() {
  9. echo "From Second Trait";
  10. }
  11. }
  12. class first_class {
  13. use first_trait, second_trait {
  14. // 在这里声明使用 first_trait 的 first_function 替换
  15. // second_trait 中声明的
  16. first_trait::first_function insteadof second_trait;
  17. }
  18. }
  19. $obj = new first_class();
  20. // Output: From First Trait
  21. $obj->first_function();

发表评论

表情:
评论列表 (有 0 条评论,21人围观)

还没有评论,来说两句吧...

相关阅读

    相关 phpTraits 详解

    PHP是单继承的语言,在PHP 5.4 Traits出现之前,PHP的类无法同时从两个基类继承属性或方法。php的Traits和Go语言的组合功能有点类似, 通过在类中使用u

    相关 PHPTrait 特性

    Trait是在PHP5.4中加入的,它既不是接口也不是类。主要是为了解决单继承语言的限制。是PHP多重继承的一种解决方案。例如,需要同时继承两个 Abstract Class,

    相关 28.特性trait.rs

    / 特性(trait)概念接近于 Java 中的接口(Interface),但两者不完全相同。特性与接口相同的地方在于它们都是一种行为规范,可以用于标识哪些类有

    相关 PHPTrait详解

    > php从以前到现在一直都是单继承的语言,无法同时从两个基类中继承属性和方法,为了解决这个问题,php出了Trait这个特性 用法:通过在类中使用use 关键字,声明要组合