-1

Every article about dependency injection in Drupal says, that you should use and that it is a best practice.

I know how to use it and I'm using it in my code. But I don't about a reason why it is best practice to use it? Why it is a best approach and why I should use it? Why we should avoid to call service as static \Drupal::service('database')?

Is it related with performance issues?

In articles which describe Dependency injection in Drupal no information about the reason why to use DI. Just only designation that everyone should use it.

Aleksandr
  • 59
  • 3
  • DI is a well known design pattern used for decoupling the usage of an object from its creation. @see [Why does one use dependency injection?](https://stackoverflow.com/q/14301389/2529954) – EricLavault Jun 01 '21 at 10:24

1 Answers1

1

Dependency Injection is a software design approach that allows avoiding hard-coding dependencies and makes it possible to change the dependencies both at runtime and compile time.

  • How it help to avoid hardcoding? If i using it as service argument or in create() method i'm anyway hardcoding it 'database' – Aleksandr Jun 03 '21 at 16:49