Php Class & 具有相同名称的特征方法

Php Class & Trait methods with same name

我有这种特殊情况,我的特征有一个方法,我的 class 有一个方法,两者同名。

我需要使用两种方法(来自特征的方法和 class)Inside that class which contains the same method

namespace Some\Namespace;
use Some\Other\Namespace\TestTrait;

class TestClass {

  use TestTrait;

  public function index()
  {
    // should call the method from the class $this->getId();
    // should also call the method from the trait $this->getId();
  }

  private function getId()
  {
    // some code
  }
}

并且在单独定义的特征中:

trait TestTrait
{
    private function getId ()
    {
        // does something
    }
}

请注意这不是粘贴代码,我可能有一些错别字 :P

使用特质Conflict Resolution

namespace Some\Namespace;
use Some\Other\Namespace\TestTrait;

class TestClass {

  use TestTrait {
      getId as traitGetId;
  }

  public function index()
  {
    $this->getId();
    $this->traitGetId();
  }

  private function getId()
  {
    // some code
  }
}