小编0CD*_*d3r的帖子

同名的PHP类和特质方法

我有这种特定的情况,我的特质有一个方法,我的班级有一个方法,两者名称相同。

我需要使用两种方法(来自特征和类的一种方法)包含相同方法的类中

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
  }
}
Run Code Online (Sandbox Code Playgroud)

并在单独定义的特征中:

trait TestTrait
{
    private function getId ()
    {
        // does something
    }
}
Run Code Online (Sandbox Code Playgroud)

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

php traits

6
推荐指数
1
解决办法
1445
查看次数

标签 统计

php ×1

traits ×1