使用特征,接口或两者?

时间:2016-10-25 12:14:04

标签: php symfony design-patterns

我有一个关于在PHP中使用Trait和Interfaces的问题。

具有foobar功能的特性

<?php

trait FoobarTrait
{
  protected $foobar;

  public function setFoobar($foobar)
  {
    $this->foobar = $foobar
  }

  public function getFoobar()
  {
    return $this->foobar;
  }
}

指定如何使用Trait的特定界面

<?php

interface FoobarInterface
{
    public function setFoobar($foobar);

    public function getFoobar();
}

我想在课堂上使用foobar功能。什么是最好的方式?

有必要使用接口实现并指定特征或它是一种诱导行为吗?

<?php

  class FoobarClass implements FoobarInterface
  {
    use FoobarTrait;
  }

或者这个

<?php

  class FoobarClass
  {
    use FoobarTrait;
  }

感谢您的回复和辩论;)

0 个答案:

没有答案