Table of Contents
【转】PHP的Trait 特性,phptrait特性
简单使用
使用多个 Trait
Trait 之间的嵌套
Trait 的抽象方法(Abstract Method)
Trait 冲突
Home Backend Development PHP Tutorial 【转】PHP的Trait 特性,phptrait特性_PHP教程

【转】PHP的Trait 特性,phptrait特性_PHP教程

Jul 12, 2016 am 08:55 AM
php trait characteristic of change

【转】PHP的Trait 特性,phptrait特性

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

简单使用

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

<span>1</span> <span>trait first_trait {
</span><span>2</span>      <span>function</span> first_method() { <span>/*</span><span> Code Here </span><span>*/</span><span> }
</span><span>3</span>      <span>function</span> second_method() { <span>/*</span><span> Code Here </span><span>*/</span><span> }
</span><span>4</span> }
Copy after login

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

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

使用多个 Trait

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

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

Trait 之间的嵌套

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

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

Trait 的抽象方法(Abstract Method)

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

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

Trait 冲突

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

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

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

  • Trait 会覆盖调用类继承的父类方法
  • Trait 无法如 Class 一样使用 new 实例化
  • 单个 Trait 可由多个 Trait 组成
  • 在单个 Class 中,可以使用多个 Trait
  • Trait 支持修饰词(modifiers),例如 final、static、abstract
  • 我们能使用 insteadof 以及 as 操作符解决 Trait 之间的冲突

文章出处:http://www.kuqin.com/web/20111119/315048.html

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1115250.htmlTechArticle【转】PHP的Trait 特性,phptrait特性 Trait是在PHP5.4中加入的,它既不是接口也不是类。主要是为了解决单继承语言的限制。是PHP多重继承的一...
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles