mardi 25 octobre 2016

Use Trait, Interfaces or both?

I have a question about using Trait and Interfaces in PHP.

A trait with foobar function

<?php

trait FoobarTrait
{
  protected $foobar;

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

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

The specific Interface to specify how to use Trait

<?php

interface FoobarInterface
{
    public function setFoobar($foobar);

    public function getFoobar();
}

I want use foobar feature in a class. What is the best way ?

It is necessary to implements with an interface and specify trait or it is an induced behavior ?

<?php

  class FoobarClass implements FoobarInterface
  {
    use FoobarTrait;
  }

Or this

<?php

  class FoobarClass
  {
    use FoobarTrait;
  }

Thank's for your reply and debate ;)

Aucun commentaire:

Enregistrer un commentaire