Rumah > php教程 > php手册 > 【转】PHP的Trait 特性,phptrait特性

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

WBOY
Lepaskan: 2016-06-13 08:43:08
asal
1080 orang telah melayarinya

【转】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> }
Salin selepas log masuk

同时,如果要在 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>
Salin selepas log masuk

使用多个 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>
Salin selepas log masuk

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>
Salin selepas log masuk

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> }
Salin selepas log masuk

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();
Salin selepas log masuk

上面就是些 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

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan