Glossary

Interface in programming

An interface describes what an object can do, not how it does it internally. This makes it possible to replace, test, or operate a collaborating part of the system differently without changing its consumer.

Short definition

A contract separates a capability from one specific class.

An interface declares public methods and their compatible signatures; an implementing class supplies its own behaviour. One class can implement multiple interfaces, and multiple classes can provide the same contract. The caller then types a dependency against the interface when the described capability is all it needs.

An interface in a programming language is not the same as a user interface or an HTTP API. All three concepts express a communication boundary, but a PHP interface is a specific language-level contract between classes. In addition to methods, a PHP interface can declare constants and, in current PHP versions, requirements for publicly accessible properties.

Use cases

Where a contract genuinely reduces coupling

An interface is useful where multiple implementations provide the same capability to the consumer, not as a mandatory addition to every class.

  • shipping providers that create shipment labels through a shared contract
  • inventory gateways for both a local system and a remote API
  • storage, cache, or queue implementations that vary by environment
  • a test double replacing a slow or paid external service
  • an application port between domain logic and infrastructure

Practical example

Creating a shipment through a shared contract

Checkout does not need to know whether the shipping provider communicates through a REST API, SOAP, or a local library. It requires only the capability to create a label. Application configuration selects the implementation; a test can use a simple fake without a network.

PHP

interface ShippingLabelProvider
{
    public function create(Shipment $shipment): ShippingLabel;
}

final class CreateShipment
{
    public function __construct(private ShippingLabelProvider $labels)
    {
    }

    public function handle(Shipment $shipment): ShippingLabel
    {
        return $this->labels->create($shipment);
    }
}

How it works

From a required capability to a concrete adapter

An interface stands between the part of the application that needs the work and the part that performs it technically.

  1. Naming the contract An interface expresses one meaningful capability, such as creating a label.
  2. Implementation A class follows the methods and their signatures but chooses its own technical implementation.
  3. Providing the dependency An application service receives the contract through its constructor or a DI container.
  4. Call The consumer works only with the interface’s methods, not the shipping provider’s details.
  5. Replacement and testing The implementation can be replaced with another adapter or a test fake while preserving the meaning of the contract.

Main components and concepts

An interface describes public behaviour, not the entire object.

A well-designed contract is narrow, stable, and speaks the language of a specific problem.

Implementation

The implements keyword commits a class to providing the required public methods with compatible signatures.

Polymorphism

Different objects can be used interchangeably when they preserve the same contract meaning for the consumer.

Multiple interfaces

A class can implement several independent capabilities. This differs from multiple class inheritance, which PHP does not support.

Interface and abstract class

An abstract class can share partial implementation and state. An interface focuses on the public contract and does not impose an inheritance branch.

Contract and API

Both are agreements between parties. A PHP interface, however, is not normally a public HTTP API for other systems.

Benefits and limitations

An interface reduces coupling, but cannot improve a poorly chosen contract.

Benefits

  • the consumer does not know the concrete technical implementation
  • replacing an implementation can have little impact on the rest of the code
  • a test is easier to isolate from external infrastructure
  • the contract makes the object’s expected capabilities more precise

Common mistakes

  • an interface for every internal class without a second consumer or alternative
  • an overly broad contract combining unrelated methods
  • an interface that copies a technical detail instead of a business need
  • assuming that an interface alone guarantees good architecture or tests

When it makes sense

Use one when the contract reflects real variability.

An interface has good reason to exist for shipping providers, payment gateways, storage, and integration clients, for example, where the application may use multiple implementations of the same capability. It is also useful when the application layer should not know database or network details.

A single internal class with no planned replacement often does not need an interface. It is better to name the specific need first; the abstraction can be added when the interface protects a real boundary, not merely a hypothetical future.

What to consider

A contract should be small, meaningful, and verifiable.

When changing an interface, consider every implementation and the meaning of the result, not only matching method names.

  • type a dependency against the smallest contract the consumer genuinely needs
  • keep parameter names consistent in implementations, especially when using named arguments
  • do not add a method merely because one unrelated implementation needs it
  • test the observable meaning of the contract across implementations
  • do not replace direct, simpler collaboration with an interface without a reason

Common questions

Interfaces in PHP

What is the difference between an interface and a class?

A class provides concrete state and behaviour. An interface primarily specifies the public contract an implementing class must follow.

Must every class have its own interface?

No. An interface makes sense for a real contract, multiple implementations, or a clear architectural boundary. For one internal class without such a need, it only adds maintenance.

Can a class implement multiple interfaces?

Yes. PHP allows a class to implement multiple interfaces; the class must then satisfy the contracts of all their public methods.

Is an interface the same as dependency injection?

No. An interface describes a contract. Dependency injection is the way an object receives a dependency from the outside; that dependency can be an interface or a concrete class.

How I design applications in practice

I introduce contracts where they simplify change or separate infrastructure.

In more complex PHP applications, I separate application decisions from databases, HTTP, and external services; I always choose the degree of separation according to the specific problem.

Request a call

I will call you on the next working day between 9:00 and 17:00.

You can also call me directly.

+420 605 181 728

Leave your phone number and send a callback request.

By sending, you agree to processing your data in order to handle your request.