Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shadrix's avatar
Level 12

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

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.

0 likes
2 replies
Drfraker's avatar

Then give it a implementation of BInterface. Just because the create function has AInterface passed in doesn't mean you have to new up SuperClass with it.

$class = new SuperClass(new ThingThatImplementsBInterface());
shadrix's avatar
Level 12

@DRFRAKER - Thanks for your suggestion. But I cannot do that. Why? Because we cannot create a new instance. Internally we don't know what is "coming" to us.

Just to give a real-life example and unfortunately the long version.

 $invoice->create(new InvoiceFoodBuyer($event->order));
 $invoice->create(new InvoiceFoodSeller($event->order));

Invoice is created for Buyer and for Seller. InvoiceFoodBuyer implements A and B Interfaces.

Inside of $invoice->create

We have a function that creates PDFs.

 (new InvoicePDF($invoiceType))->createPdf();

$invoiceType should implement B interfaces,

However everything in $invoice->create() should implement A interfaces...

Please or to participate in this conversation.