It seems that in Drupal code anything that is neither hook nor Plugin nor Controller nor any other thing with a standard functionality should be a Service.

For sure, services make sense when we want to give other modules the possibility to change the functionality that we are writing.

But I can think of a lot of scenarios where it is absolutely sufficient to have a normal class. When we just want to create an instance, call a method, nothing else.

How can I decide if a class is sufficient, or if a service makes sense?

Edit This is not opinion based. There must be a clear and objective reason why we use services in Drupal, and I want to know this reason.

  • 2
    I think this will probably be closed as opinion based (there isn't really a way to give an objective, correct answer), but some related reading: drupal.stackexchange.com/questions/251111/…, drupal.org/docs/drupal-apis/services-and-dependency-injection/…
    – Clive
    Jul 27, 2020 at 16:17
  • 2
    It's really up to the code author intent. When you're looking to create globally-available, encapsulated black-box functionality, that may depend on other services it's a good sign that you might want to create a service. Re: Service class vs "normal" class, it helps to think about what you're encapsulating. A service class is encapsulating functionality that a service provides whereas a "normal" class (e.g. Node) is encapsulating some data along with functionality to act upon it. It might help to read up on SOA to get a better grasp of the design paradigm.
    – Shawn Conn
    Jul 27, 2020 at 16:30
  • 5
    For me the main difference is that a service is by default a singleton staying in memory the entire request. So the scope of a service is global while a class instance you create is local.
    – 4uk4
    Jul 27, 2020 at 16:53
  • 1
    @NoSssweat, Hehe, the question is slightly different, however, your answer to that question is very good, and it helps me a lot. Thank you! Jul 27, 2020 at 21:16


Browse other questions tagged or ask your own question.