DEV Community

István Döbrentei
István Döbrentei

Posted on

Building a PSR-Compliant Microservice in Pure PHP - Part 1

My main goal is to write a microservice in pure PHP without using any frameworks. I’m doing this purely for educational purposes — to learn more about the underlying technologies and the PHP language itself. In this first part of the article, I define some important terms, principles, and related technologies.

What Is a Microservice?

A microservice is an architectural style for designing applications by dividing them into small, independent services that each perform a specific task.

Key characteristics of a microservice:

  • It can be developed, updated, deployed, and scaled independently of other parts of the application.

  • Each service can use different programming languages, databases, or frameworks.

  • The focus is on a single responsibility or domain.

  • Services communicate via well-defined APIs over HTTP (e.g., REST).

  • Each service can have its own dedicated data store and is responsible for its own data consistency.

Requirements for My Microservice

  • Support an HTTP REST API

  • Be PSR-compliant

  • Apply the Separation of Concerns principle

  • Be written in pure PHP 8.x, without any frameworks

PSR Standards Used

PSR stands for PHP Standard Recommendation. A modern PHP application should follow PSR standards to ensure the code is reliable, maintainable, and compatible with the broader PHP ecosystem.

My microservice will follow these standards:

  • PSR-1: Basic Coding Standard

  • PSR-3: Logger Interface

  • PSR-4: Autoloader Standard

  • PSR-7: HTTP Message Interface

  • PSR-11: Container Interface

  • PSR-12: Extended Coding Style Guide

  • PSR-17: HTTP Factory Interfaces

Using PSRs helps ensure that the code is professional and interoperable with other libraries and tools.

What Does "Single Responsibility" Mean?

Single Responsibility Principle (SRP) is part of the SOLID principles. It states that a class, function, or module should have only one reason to change — that is, it should have only one responsibility.

When a component handles multiple responsibilities, it becomes harder to understand, test, and maintain. While this principle seems obvious in theory, I still see many real-world examples where it is violated — everything is often crammed into a single large class instead of being broken into smaller, focused components.

What Is Separation of Concerns (SoC)?

Separation of Concerns is a higher-level architectural principle that complements SRP. While SRP helps with structuring code at the micro level, SoC guides the macro-level design of systems.

SoC promotes the idea that different concerns (e.g., routing, business logic, data access) should be handled in separate modules or layers of the application. SRP is one of the tools you use to achieve SoC.

What Is a REST API?

A REST API (Representational State Transfer Application Programming Interface) is a standardized way for different software systems to communicate over the web using HTTP.

Key features of REST APIs:

  • Stateless – Each request is independent; the server doesn’t remember previous interactions.

  • Resource-based – Everything is treated as a resource, identified by a URL.

  • Uniform interface – REST APIs follow a predictable structure and conventions.

  • Standard formats – Data is typically returned in JSON or XML.

In the next part of the article, I’ll define the concrete requirements and design some UML structures for the microservice.

Top comments (0)