shadrix
5 months ago
1238
2
Tips

How to avoid Interface Segregation in my case? (SOLID)

Posted 5 months ago by shadrix

Hey hey :)

I'm trying to use the SOLID-principles, however, I have a problem with Type-Hinting.

First, have a look at my code, but it is important to note: $a implements AInterface and BInterface.

public function create(AInterface $a): void
{
  ...
  $class = new SuperClass($a); //---> here is the problem, it wants BInterface, not AInterface
  ..
}

For the first create method, we just care about the AInterface-Methods

Inside of SuperClass we just care about the BInterface-Methods

However, my intelephense writes, that the type is wrong.

Currently, I just merged AInterface and BInterface together, but Interface Segregation states that we should not force to use a method when not used.

How would you do this?

I've looked it up and some solutions are using a third interface, extending AInterface and BInterface.

Please sign in or create an account to participate in this conversation.