LoginSignup
1
2

More than 5 years have passed since last update.

phpmdでエラーになったTraitの使用例

Last updated at Posted at 2012-11-27

以下のような実装をした時に

  • php的にはエラーにはならず、期待する動作をした
  • でも、phpmd(1.4.0)でエラーになった

という事が起きて、Jenkinsさんに怒られた…。

<?php

trait TraitA
{
    function executeTraitA()
    {
        echo __METHOD__, PHP_EOL;
        $this->show();
    }

    abstract function show();
}

trait TraitB
{
    function executeTraitB()
    {
        echo __METHOD__, PHP_EOL;
        $this->show();
    }

    abstract function show();
}

class Test
{
    use TraitA;
    use TraitB;

    function show()
    {
        echo __METHOD__, PHP_EOL;
    }
}

$obj = new Test;
$obj->executeTraitA();
$obj->executeTraitB();
there are collisions with other trait methods on ... 

という感じでエラーを吐く。

abstractで手を抜くのではなくinterface使ったりしなさいという事か…?

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2