r/PHPhelp • u/Spiritual_Cycle_3263 • Mar 11 '26
Naming Interfaces without the Interface suffix for infrastructure services
There has been a trend in PHP to drop the Interface suffix so type hints read more naturally. For example, using Vehicleinstead of VehicleInterface, since code requests a vehicle, not an interface. This works well for domain abstractions, but it becomes tricky with infrastructure services such as containers, loggers, caches, or mailers, where both the abstraction and the default implementation naturally want the same name.
Different frameworks handle this in distinct ways. Laravel framework places interfaces in a Contracts namespace (e.g., Contracts\Container\Container) while the concrete implementation lives elsewhere. To use them in code, developers often have to alias the interface (use Contracts\Container\Container as ContainerContract), which can lead to a crowded top-of-file section. Symfony framework generally keeps the Interface suffix (e.g., LoggerInterface, ContainerInterface), avoiding naming conflicts. Tempest PHP framework gives the interface the natural name (Container) and names almost all concrete implementations with Generic (e.g., GenericContainer), which keeps the code clean but requires less intuitive implementation names.
For developers building frameworks or reusable libraries, how do you typically approach this? Do you keep the suffix for infrastructure contracts, use a contracts namespace with aliases, adopt a “Generic” naming scheme, or follow another pattern.
5
u/martinbean Mar 11 '26
I seldom run into this problem, as I usually have an interface that generically describes something (i.e.
NewsletterService) but then specific implementations (e.g.MailchimpNewsletterService,CampaignMonitorNewsletterService, etc).Even with a component in a framework or library I’d still expect different implementations. So taking Laravel, it has a queue component, so if you were building something similar you’d have some sort of
MessageQueueinterface loosely describing such a service, and then specific implementations of that interface (SqsMessageQueue,RedisMessageQueue, etc).If you’re just defining interfaces with the same name as classes, just to “depend on an interface, not an implementation” then I’d ask what’s the point? Sure, the SOLID principles are good principles to follow, but there has to be a point where you’re actually pragmatic and ask yourself, “Is this worth it?” or, “Is this actually making my code better?” Creating an interface for one class, and only ever having one implementation of that interface, just feels overkill to me.
4
u/eurosat7 Mar 11 '26 edited Mar 11 '26
I keep em. I have auto completion. (I am mostly in the symfony bubble)
We tried to remove them - did not work well. And it felt strange sometimes. We even lost time as we had to look up if something was in interface.
7
u/Crafty-Pool7864 Mar 11 '26
I prefer consistency so just live with appending Interface as a suffix.
Far from a hill to die on though.
2
u/Commercial_Echo923 Mar 11 '26
Dropping interface suffix is one of those things which sound very reasonable at first but in the end its just easier and more convinient to keep it for everyone. At least thats my opinion.
In the end your `Vehicle` is still not an actual vehicle too, lol.
1
u/itemluminouswadison Mar 11 '26
Interface suffix I think makes the most sense still. Having a separate dir for just contracts feels less portable somehow
3
u/__kkk1337__ Mar 11 '26
I prefer Interface suffix, this is really clean way
1
u/rmb32 Mar 11 '26
An interface is a placeholder for an implementation. The interface “completes the puzzle” regardless of implementation. I’m less inclined over time to append them with “Interface”. Instead I would prefix the concrete implementations with an appropriate adjective.
If you’re into DDD or just careful about layers then each layer should be fully “complete” by relying on interfaces in its own layer, but the classes that implement those interfaces often exist in other layers.
For packages or framework code that uses the “Interface” suffix, I tend to wrap those anyway and never rely directly on 3rd party code.
1
u/YahenP Mar 12 '26
I miss Hungarian notation.
1
u/silentkode26 Mar 12 '26
Putting interface in contracts namespace does st the end the same thing. Separate interface and implementation. Also I can create Translator class and load dictionary from file. When future arises the need for second implementation loading dictionary from database I change Translator to interface and create two classes that implements it FileTranslator and DbTranslator. Renaming it to TranslatorInterface would introduce breaking change, so major release instead of minor (new feature). DI will solve the bootstrapping part.
1
u/hennell Mar 13 '26
In general if your interface and a concrete implementation want the same name you interface is too specific or your implementation is named too generically. `Vehicle` is not a good name for an actual class..
But where the interface is writen more for extendability then because you actually have multiple classes the names are weird to find diffrenent values for.
In abstract I'd call it Interface and call it a day as that's pretty clear what it is and why. In practice though I'd follow whatever the convention is of the framework/code I'm working in. For Laravel I do have my own contract folders, although I can't think of any time I alias them, you reference the contract and then resolve the actual class from the container.