r/PHPhelp • u/Fluent_Press2050 • Jan 21 '26
How does PHP handle Interface looping?
Let's say you have 2 interfaces and 2 classes like this:
interface ExceptionInterface extends \Throwable
interface DomainExceptionInterface extends ExceptionInterface
class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
class DomainArgumentException extends InvalidArgumentException implements DomainExceptionInterfaceInvalidArgumentException and DomainArgumentException essentially both end up using ExceptionInterface at the end.
Does this cause an issue with PHP or is this allowed?
2
Upvotes
7
u/DrDam8584 Jan 21 '26
Have you test it ?
2
u/mike_a_oc Jan 21 '26
Seems fine to me. I can't think of why that wouldn't work.
That said, the InvalidArgumentException will blow up unless you are putting it into its own namespace.
It would have to look like
2
u/Vroomped Jan 21 '26
PHP always evaluates top to bottom in that order. It's in the standard, despite some popular tools going off the book. Officially, your example is fine.
1
u/the-fluent-developer Jan 22 '26
Why should this not work? See https://3v4l.org/YfEj4#veol
1
u/insight_designs Jan 27 '26
This is completely valid PHP. PHP handles this gracefully. A class can "arrive at" the same interface through multiple paths (parent class, direct implementation, interface inheritance) and PHP just deduplicates it.
Your DomainArgumentException implements ExceptionInterface via:
PHP doesn't care. It just checks that all required method contracts are satisfied, and it only needs them satisfied once.