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> }
同时,如果要在 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>
在同个 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>
同时,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>
我们可以在 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> }
多个 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();
上面就是些 Trait 比较基本的使用了,更详细的可以参考官方手册。这里总结下注意的几 点:
文章出处:http://www.kuqin.com/web/20111119/315048.html