PHP中的Trait 特性及作用

作者:赵亚飞 时间:2023-11-21 08:04:15 

自 PHP 5.4.0 起,PHP 实现了代码复用的一个方法,称为 traits。

Traits 是一种为类似 PHP 的单继承语言而准备的代码复用机制。Trait 为了减少单继承语言的限制,使开发人员能够自由地在不同层次结构内独立的类中复用方法集。Traits 和类组合的语义是定义了一种方式来减少复杂性,避免传统多继承和混入类(Mixin)相关的典型问题。

Trait 和一个类相似,但仅仅旨在用细粒度和一致的方式来组合功能。Trait 不能通过它自身来实例化。它为传统继承增加了水平特性的组合;也就是说,应用类的成员不需要继承。

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

简单使用

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


trait first_trait {
function first_method() { /* Code Here */ }
function second_method() { /* Code Here */ }
}

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


class first_class {
// 注意这行,声明使用 first_trait
use first_trait;
}
$obj = new first_class();
// Executing the method from trait
$obj->first_method(); // valid
$obj->second_method(); // valid

使用多个 Trait

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


trait first_trait
{
function first_method() { echo "method"; }
}
trait second_trait {
function second_method() { echo "method"; }
}
class first_class {
// now using more than one trait
use first_trait, second_trait;
}
$obj= new first_class();
// Valid
$obj->first_method(); // Print : method
// Valid
$obj->second_method(); // Print : method

Trait 之间的嵌套

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


trait first_trait {
function first_method() { echo "method"; }
}
trait second_trait {
use first_trait;
function second_method() { echo "method"; }
}
class first_class {
// now using
use second_trait;
}
$obj= new first_class();
// Valid
$obj->first_method(); // Print : method
// Valid
$obj->second_method(); // Print : method

Trait 的抽象方法(Abstract Method)

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


trait first_trait {
function first_method() { echo "method"; }
// 这里可以加入修饰符,说明调用类必须实现它
abstract public function second_method();
}
class first_method {
use first_trait;
function second_method() {
/* Code Here */
}
}

Trait 冲突

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


trait first_trait {
function first_function() {
echo "From First Trait";
}
}
trait second_trait {
// 这里的名称和 first_trait 一样,会有冲突
function first_function() {
echo "From Second Trait";
}
}
class first_class {
use first_trait, second_trait {
// 在这里声明使用 first_trait 的 first_function 替换
// second_trait 中声明的
first_trait::first_function insteadof second_trait;
}
}
$obj = new first_class();
// Output: From First Trait
$obj->first_function();

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

Trait 会覆盖调用类继承的父类方法

Trait 无法如 Class 一样使用 new 实例化

单个 Trait 可由多个 Trait 组成

在单个 Class 中,可以使用多个 Trait

Trait 支持修饰词(modifiers),例如 final、static、abstract

我们能使用 insteadof 以及 as 操作符解决 Trait 之间的冲突

标签:php,trait
0
投稿

猜你喜欢

  • ADO.NET实现对SQL Server数据库的增删改查示例

    2024-01-12 17:37:54
  • vue中provide和inject的用法及说明(vue组件爷孙传值)

    2024-05-21 10:15:26
  • python3 property装饰器实现原理与用法示例

    2021-07-10 05:02:28
  • 基于ORA-19815闪回空间爆满问题的处理方法

    2024-01-21 03:35:04
  • Python selenium根据class定位页面元素的方法

    2021-04-03 08:17:39
  • Python快速生成定制化的Word(docx)文档

    2022-04-27 12:33:06
  • Redis持久化机制实现原理和流程

    2021-03-22 00:43:39
  • 简单的XML操作:XML文件创建

    2008-04-25 10:31:00
  • 深入PHP magic quotes的详解

    2024-05-11 10:12:00
  • jupyter 使用Pillow包显示图像时inline显示方式

    2021-09-08 00:42:02
  • Python OpenGL绘制一场烟花盛会

    2021-02-08 06:12:44
  • YUI3设计中的激进和妥协

    2010-01-17 09:59:00
  • MySQL8.0服务无法正常启动的解决过程

    2024-01-13 04:33:27
  • Python-OpenCV实现图像缺陷检测的实例

    2023-02-16 19:38:46
  • python实现知乎高颜值图片爬取

    2023-03-11 10:35:54
  • 使用Canal实现PHP应用程序与MySQL数据库的实时数据同步

    2023-05-25 01:54:39
  • 详解SQL Server的聚焦过滤索引

    2024-01-22 06:15:23
  • python中文件操作与异常的处理图文详解

    2021-09-04 16:04:33
  • js点击事件的执行过程实例分析【冒泡与捕获】

    2024-02-27 07:53:34
  • MySQL的root密码丢失解决方法

    2011-05-05 15:56:00
  • asp之家 网络编程 m.aspxhome.com