官术网_书友最值得收藏!

What is a service?

A service is an object that gets instantiated by a Service Container and is used to handle operations in a reusable way, for example, performing calculations and interacting with the database, an external API, or any number of things. Moreover, it can take dependencies (other services) and use them to help out. Services are a core part of the dependency injection (DI) principle that is commonly used in modern PHP applications and in Drupal 8.

If you don't have any experience with these concepts, an important thing to note is also that they are globally registered with the service container and instantiated only once per request. This means that altering them after you requested them from the container means that they stay altered even if you request them again. In essence, they are singletons. So, you should write your services in such a way that they stay immutable, and most of the data they need to process is either from a dependency or passed in from the client that uses it (and does not affect it).

Many Drupal 8 core service definitions can be found inside the core.services.yml file located in the root /core folder. So, if you are ever looking for service names to use, your best bet is to look there. Additionally, core modules also have service definitions inside their respective *.services.yml files. So, make sure that you also check there.
主站蜘蛛池模板: 孝义市| 海安县| 临江市| 沂源县| 罗定市| 普定县| 台东县| 沛县| 裕民县| 黄梅县| 嘉黎县| 同江市| 韶关市| 山丹县| 娄底市| 安顺市| 灵丘县| 琼结县| 建昌县| 清镇市| 山东| 淮阳县| 宁德市| 炉霍县| 金坛市| 新干县| 邢台市| 周至县| 桐柏县| 眉山市| 双江| 普兰店市| 浦城县| 南川市| 津市市| 库伦旗| 龙泉市| 广汉市| 普安县| 德江县| 电白县|