zoukankan      html  css  js  c++  java
  • 【转】PHP的Trait 特性

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

    简单使用

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

    1 trait first_trait {
    2      function first_method() { /* Code Here */ }
    3      function second_method() { /* Code Here */ }
    4 }

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

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

    使用多个 Trait

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

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

    Trait 之间的嵌套

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

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

    Trait 的抽象方法(Abstract Method)

    我们可以在 Trait 中声明需要实现的抽象方法,这样能使使用它的 Class 必须实现它

     1 trait first_trait {
     2     function first_method() { echo "method1"; }
     3 
     4     // 这里可以加入修饰符,说明调用类必须实现它
     5     abstract public function second_method();
     6 }
     7 
     8 class first_method {
     9     use first_trait;
    10 
    11     function second_method() {
    12         /* Code Here */
    13     }
    14 }

    Trait 冲突

    多个 Trait 之间同时使用难免会冲突,这需要我们去解决。PHP5.4 从语法方面带入了相关 的关键字语法:insteadof 以及 as ,用法参见

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

    上面就是些 Trait 比较基本的使用了,更详细的可以参考官方手册。这里总结下注意的几 点:

    • Trait 会覆盖调用类继承的父类方法
    • Trait 无法如 Class 一样使用 new 实例化
    • 单个 Trait 可由多个 Trait 组成
    • 在单个 Class 中,可以使用多个 Trait
    • Trait 支持修饰词(modifiers),例如 final、static、abstract
    • 我们能使用 insteadof 以及 as 操作符解决 Trait 之间的冲突
    注意:
    1、本博客同步更新到我的个人网站:http://www.zhaoyafei.cn
    2、本文非原创内容,为了尊重他人劳动,转载请到文章原文地址:
    http://www.kuqin.com/web/20111119/315048.html
     
  • 相关阅读:
    iOS中block的探究
    输出IOS 版本号
    Luke's Homepage
    ObjectiveC: delegate的那点事儿
    浅谈XCode编译器的Blocks功能
    一个横版的tableViewFantasyView
    iOS中block的探究
    NSRunLoop 概述和原理
    Block使用中的一些疑问解答
    Flex 中的注释
  • 原文地址:https://www.cnblogs.com/zyf-zhaoyafei/p/5341223.html
Copyright © 2011-2022 走看看