How and when should we use Interface to document systems in PHP?

Always wanted to know exactly, if it is a good practice, to make a system always making use of Interfaces, or is this not necessary?

Interface People {
  public function getName();
  public function setName($string);
  public function getAge();
  public function setAge($string);
}

class User implements People {

  private $name;
  private $age;

  public function __construct()
  {
   return $this; 
  }

  public function setName($string)
  {
     $this->name = $string;
     return $this;
  }

  public function getName()
  {
    return $this->name;
  }

  public function setAge($number)
  {
    $this->age = $number;
    return $this;
  }

  public function getAge()
  {
    return $this->age;
  }

}
Author: Ivan Ferrer, 2015-11-26

2 answers

Necessary is not, but it is good for several reasons, documenting really is one of them, giving more robustness in the code is another.

I just don't quite understand how dynamic typing languages invest so much in this, it seems to go against their own philosophy of letting tests or programmer intuition detect errors. Even worse when the language is weak typing.

The interface is a contract that must be followed when making an implementation. Data typing is also a contract. I don't know why they ignore one way and follow another. Perhaps the languages themselves are gradually admitting that they used a misspelling philosophy, at least for the most important purposes.

There are languages of script and languages enterprise, among others, PHP was a language of script that now wants to be a language enterprise, but the legacy does not let this transition.

Then on a higher level one must ask: why Will you use a protective measure in a language that leaves aside other more important protective measures? Few ask themselves this question, and many who ask it seriously and can answer it, will decide to change language.

Of course some protective measure is better than nothing, it is advantageous in itself, not only for this.

It also generalizes (abstracts) what it is intending to do, and that's good. This is an even better reason to use interface. I will not explain in detail here because already there is an answer about this .

In short, when we have the interface implemented we can use it to indicate that only what is present in it is what we need. And that any class that implements it can be used concretely to satisfy a specific algorithm. This facilitates the maintenance and configuration of the application. You don't stick to concrete implementation. The advantages are described in the question linked above.

Other more coherent script languages prefer duck typing.

See more at What is typing style?.

 13
Author: Maniero, 2020-10-08 11:32:59

The main advantage I see of this in PHP is the question of offering flexibility to the programmer in the case of using a library where he implements his own resource.

In this case, the programmer could simply use his own class, provided that he implements the interface required to perform such an operation.

An example: these days, I was having trouble with the Facebook library, as I use Laravel, and Laravel does not use the native PHP session. The Facebook uses the native PHP session. The result is that I was having trouble.

The class of Facebook implements an interface called PersistentDataInterface. This interface had the set and get methods needed to determine how data would be saved in the session.

Since data typing is done through PersistentDataInterface, i implemented it, making the methods save and get the data straight from the Laravel session.

So everything worked properly.

So see an example:

interface PersistentData {
    public function set($key, $value);
    public function get($key);
}

class Library{
      public function __construct(PersistentData $persist)
      {
            $this->persist = $persist;
      } 
}

class ClasseQueNaoAtendeMeuSistema implements PersistentData
{
     public function set($key, $value) { ... }
     public function get($key);
}

See in this case that the construtor of the Library class requires the typing to be the implementation of interface, and not the classe itself. Therefore, this gives more flexibility for me to create another class with the interface methods, but that do the persistence of the data differently!

I think using the interface is a good practice when you want to offer more ways your library can interact with other classes. How do you" force " the class third to use the methods through the interface, you can call a method of the object passed by parameter without "being afraid" that the method does not exist and having to make thousands of ifs with method_exists.

THE FUTURE

In PHP7 it will be wonderful to combine the feature of interfaces with the classes anônimas, as this gives even more flexibility. See for example, in the case above, what could be done.

$tempPersist = new class(Database::connection()) implements PersistentData
{
    public function __construct(Database $database)
    {
         $this->database = $database;
    }
    public function set($key, $value)
    {
       $this->database->table('session')->save([$key=>$value]);
    }  

    public function get($key)
    {
         return $this->database->table('session')->get($key);
    }
   
}


 $lib = new Library($tempPersist);
 6
Author: Wallace Maxters, 2020-06-11 14:45:34