dsg41888 2013-12-04 17:41
浏览 60
已采纳

PHP特征方法冲突:特质“继承”和特征层次结构

UPDATE: I am not alone in my pondering on this issue and it seems it is indeed a bug. See here. The day it is fixed is going to be a fantastic day! :)


This started out as I love PHP traits! I'm going to use them everywhere! ^_^ and now it has turned into a Thought Exercise / Learning Experience >_<.

Consider the following example:

trait TheErrorOfYourWays{

   public function booboo(){
       echo 'You had a booboo :(';
   }
}

trait SpectacularStuff1 {
    use TheErrorOfYourWays; 
}

trait SpectacularStuff2 {
    use TheErrorOfYourWays;
}

class DoSomethingSpectacular {
    use SpectacularStuff1, SpectacularStuff2;
}

This results in (obviously not so obviously):

Fatal error: Trait method booboo has not been applied, because there are collisions with other trait methods on DoSomethingSpectacular.

So my question: How do I resolve method conflicts in traits? Is it possible to achieve overlapping trait "inheritance"? If so, what is the "right" way to do this?

Why I want to do this:

  1. I want to create self contained traits and classes (mix and match style). If it is at all possible, I want to say "use" and then magic stuff must happen. No having to scratch my head and think, "Now what namespace was that trait in again?", etc, etc.
  2. No having to edit classes and traits on the fly when I do something "adventurous" and discover I have inadvertently created a conflict.
  3. Seemed like a good idea at the time.

What I have tried:

  1. The PHP Manual.
  2. The Google.
  3. SO including this question -> Not the correct answer for this scenario.
  4. Found this but I am using PHP version 5.5.1. It's fixed, right? Right?
  5. A fantastic array of "as", aliases, even insteadof, in different places, times, universes, etc. Including, but not limited to:

    trait SpectacularStuff1 {
       use TheErrorOfYourWays{
          TheErrorOfYourWays::booboo as booboo1;
       }
    }
    trait SpectacularStuff2 {
       use TheErrorOfYourWays{
          TheErrorOfYourWays::booboo as booboo2;
       }
    }
    class DoSomethingSpectacular {
       use SpectacularStuff1, SpectacularStuff2 {
          /* Tried separately, but included here for brevity's sake */
          SpectacularStuff1::booboo as booboo3;
          SpectacularStuff2::booboo as booboo4;
       }
    }
    

    AND

    use TheErrorOfYourWays as Erroneous1;
    trait SpectacularStuff1 {
        use Erroneous1{
            Erroneous1::booboo as booboo1;
        }
    }
    
    use TheErrorOfYourWays as Erroneous2;
    trait SpectacularStuff2 {
        use Erroneous2{
            Erroneous2::booboo as booboo2;
        }
    }
    

I understand that:

  1. I can change TheErrorOfYourWays to a class and make booboo() static but I would like to learn about this specific trait behaviour.
  2. I can remove TheErrorOfYourWays from the traits and use it in the class, but that's hardly "self-contained" then. Everytime I use the traits I have to remember to use TheErrorOfYourWays in the class even if I don't call booboo() directly from the class. Sounds dangerous.
  3. I have probably made some rookie syntax error or failed to understand aliasing on a profound level. If so, please... explain... slowly...
  4. There is probably a better way to do this. If so, please... explain... slowly...
  5. I may be prematurely enthusiastic and PHP doesn't do this yet. Let me down gently.

Thanks!

  • 写回答

4条回答 默认 最新

  • doubi1624 2013-12-07 09:59
    关注

    So the unofficial "official" answer is:

    You can do it without aliasing, insteadof or anything! But not yet...

    I upgraded from 5.5.1 to 5.5.6 but 'twas all in vain. If I remember, I will update this answer when the fix becomes available. For something kind of like a work-around, interesting to note is that you can call trait static functions directly. The following example works:

    trait TheErrorOfYourWays{
        public static function booboo($thisTrait){
            echo 'You had a booboo :( in '.$thisTrait.'<br>';
        }
    }
    
    trait SpectacularStuff1 {
        public function boobooTest1(){
            TheErrorOfYourWays::booboo(__TRAIT__);
        }
    }
    
    trait SpectacularStuff2 {
        public function boobooTest2(){
            TheErrorOfYourWays::booboo(__TRAIT__);
        }
    }
    
    class DoSomethingSpectacular {
        use SpectacularStuff1, SpectacularStuff2;
    }
    
    $boobooAChoo = new DoSomethingSpectacular();
    $boobooAChoo->boobooTest1(); // You had a booboo :( in SpectacularStuff1
    $boobooAChoo->boobooTest2(); // You had a booboo :( in SpectacularStuff2
    

    Yes, yes, you can also do that with a class but classes are soooo last season.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题