Search⌘ K
AI Features

Service Classes

Explore the concept of service classes in PHP web applications. Understand how to design services that encapsulate domain and application logic while ensuring they are easy to use, test, and modify. Learn principles like SOLID, dependency injection, avoiding state, and separating side effects to improve code predictability and maintainability.

What are services?

Services are the classes containing domain and application logic. Every service provides an API to trigger some actions, be it something small like formatting data or something big like creating orders.

Services are also notable because they don’t care what calls them. Each service encapsulates an operation that can be run by a controller, console command, test, or another service.

How to make good services

We want to make good services. But, to achieve this, we first need to understand what a good service means. A good service is:

  • Easy to use.
  • Easy to test.
  • Easy to modify when requirements change.

Another distinctive feature of good service is predictability. We should be able to tell what a service does just by looking at its external API and without knowing how the service works inside.

It should be true predictability. Sometimes the service API looks simple but doesn’t ...