PHP中特征的作用是什么

  介绍

这篇文章将为大家详细讲解有关PHP中特征的作用是什么,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

PHP是单继承的语言,在PHP 5.4特征出现之前,PHP的类无法同时从两个基类继承属性或方法。PHP的特质和去语言的组合功能类似,通过在类中使用使用关键字声明要组合的特征名称,而具体某个特质的声明使用特征关键词,特征不能直接实例化。具体用法请看下面的代码:

& lt; PHP ?   trait 才能;Drive  {   ,,,public  carName 美元;=,& # 39;特征# 39;;   ,,,public  function 驾驶(),{   ,,,,,echo “driving  {$ this→carName} \ n";   ,,,}   ,,}   class 才能;Person  {   ,,,public  function 吃(),{   ,,,,,echo “吃\ n";   ,,,}   ,,}   class 才能Student  extends  Person  {   ,,,use 驱动;   ,,,public  function 研究(),{   ,,,,,echo “研究\ n";   ,,,}   ,,}   时间=美元才能student  new 学生();   学生→美元才能研究();   学生→美元才能吃();   学生→美元才能驾驶();

输出结果如下:

研究   吃   driving 特质

上面的例子中,学生类通过继承人,有了吃的方法,通过组合,有了驾驶方法和属性carName。

如果特征,基类和本类中都存在某个同名的属性或者方法,最终会保留哪一个呢?通过下面的代码测试一下:

& lt; ? php    trait 才能;Drive  {   ,,,public  function  hello (), {   ,,,,,echo “hello 驱动\ n";   ,,,}   ,,,public  function 驾驶(),{   ,,,,,echo “driving ,得到驱动\ n";   ,,,}   ,,}   class 才能;Person  {   ,,,public  function  hello (), {   ,,,,,echo “hello 人\ n";   ,,,}   ,,,public  function 驾驶(),{   ,,,,,echo “driving 得到人\ n";   ,,,}   ,,}   class 才能Student  extends  Person  {   ,,,use 驱动;   ,,,public  function  hello (), {   ,,,,,echo “hello 学生\ n";   ,,,}   ,,}   时间=美元才能student  new 学生();   学生→美元才能hello ();   学生→美元才能驾驶();

输出结果如下:

hello 学生   driving 得到传动

因此得出结论:当方法或属性同名时,当前类中的方法会覆盖特征的方法,而特征的方法又覆盖了基类中的方法。

如果要组合多个特征,通过逗号分隔特征名称:

使用Trait1 Trait2;

如果多个特征中包含同名方法或者属性时,会怎样呢?答案是当组合的多个特征包含同名属性或者方法时,需要明确声明解决冲突,否则会产生一个致命错误。

& lt; php ?   trait  Trait1  {   public 才能;function  hello (), {   ,,,echo “Trait1:你好\ n";   ,,}   public 才能;function 嗨(),{   ,,,echo “Trait1:你好\ n";   ,,}   }   trait  Trait2  {   public 才能;function  hello (), {   ,,,echo “Trait2:你好\ n";   ,,}   public 才能;function 嗨(),{   ,,,echo “Trait2:你好\ n";   ,,}   }   class  Class1  {   use 才能;Trait1, Trait2;   }

输出结果如下:

PHP  Fatal 错误:,Trait  method  hello  has  not  been ,, because  there 断开连接;collisions  with  other  Trait  methods 提醒Class1 拷贝~/php54/trait_3.php 提醒line  20

使用可和作为操作符来解决冲突,传播是使用某个方法替代另一个,而因为是给方法取一个别名,具体用法请看代码:

& lt; php ?   trait  Trait1  {   public 才能;function  hello (), {   ,,,echo “Trait1:你好\ n";   ,,}   public 才能;function 嗨(),{   ,,,echo “Trait1:你好\ n";   ,,}   }   trait  Trait2  {   public 才能;function  hello (), {   ,,,echo “Trait2:你好\ n";   ,,}   public 才能;function 嗨(),{   ,,,echo “Trait2:你好\ n";   ,,}   }   class  Class1  {   use 才能;Trait1, Trait2  {   ,,,Trait2: hello  insteadof  Trait1;   ,,,Trait1: hi  insteadof  Trait2;   ,,}   }   class  Class2  {   use 才能;Trait1, Trait2  {   ,,,Trait2: hello  insteadof  Trait1;   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

PHP中特征的作用是什么