PHP Interface Segregation Principle – Type Hinting Multiple Interfaces

dependenciesinterfacesPHPsolid

The question is about typehinting of the role interfaces.

The code example has two DataMappers which typehint the interface they need in the constructor,

FooDataMapper type hints the Preparable interface. Because it dependens on it and no other method from the dependency.

In the other mapper, BarDataMapper i replaced the type hint with ??? as it requires both the Preparable and the Queryable interface.

How do i handle this (what should replace ???), typehinting a single of them would not suffice, should i make a new interface (maybe one that extends the previous mentioned) and hint this new interface?

The code example is a boiled down example in PHP and the type hint (???) is in the last Class declared.

<?php
// TWO ROLE INTERFACES, SMALL SEGREGATED PART OF A DBAL

Interface Preparable {
  public function prepare( .. );
}

Interface Queryable {
  public function query( ... );
}


// ADAPTER IMPLEMENTS SEGREGATION

class DbalAdapter implements Preparable, Queryable {
  private $dbal;

  public function __construct($dbal) 
  {
    private $this->dbal = $dbal;
  }

  public function prepare( .. ) 
  {
    ..
    return $this->dbal->prepare( .. );
  }

  public function query( .. ) 
  {
    ..
    return $this->dbal->query( .. );
  }
}


// DATAMAPPER CTOR HINTS FOR METHODS IT NEEDS TO USE

class FooDataMapper implements DataMapper {
  private $dbal;

  public function __construct(Preparable $dbal)
  {
    $this->dbal = $dbal;
  }

  public function create(Foo $Foo) 
  {
    ..
    $this->dbal->prepare( .. )
  }
}


// DATAMAPPER CTOR HINTS FOR METHODS IT NEEDS TO USE
// BUT THIS TIME NONE OF THE ROLE INTERFACES MATCH, BUILD A NEW INTERFACE?

class BarDataMapper implements DataMapper {
  private $dbal;
  public function __construct(??? $dbal) 
  {
    $this->dbal = $dbal;
  }

  public function SomeMethodThatNeedsBothPrepareNQuery(Bar $Bar) 
  {
    ..
    $this->dbal->query( .. )
    $this->dbal->prepare( .. )
  }
}

The initial solution i come to is making a new interface extending the ones needed.

Beware the naming of classes in the code are only for shown, might as well have been Fooable, BarAble…

Best Answer

The consumer of dependencies is the one responsible for defining them (by means of their interfaces). In your example, BarDataMapper rules. I see two possible scenarios:

  1. If BarDataMapper depends on a single object implementing both prepare() and query() methods, then yes, a proper PreparableAndQueryable interface should be defined including the two of them, and passed to the constructor. But then, perhaps you want to find another meaningful, non-composed name not hinting at bad separation of concerns.

  2. If it makes sense for BarDataMapper to consume independent Preparable and Queryable dependencies, then its constructor takes them as separate parameters, even if in the end there's really one single object implementing both interfaces. Who's really implementing interfaces should not bother the one consuming them.

Related Topic