且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在自定义类中获取服务(依赖项)

更新时间:2022-12-11 17:34:14

将自定义类定义为服务,然后向其中注入依赖项.

Define your custom class as a service, and then inject dependencies into it.

例如:

// services.yml
services:
   my.custom.service.id:
       class: My\Custom\Class
       arguments:
         - @service.name
         - @doctrine.orm.entity_manager

然后您的自定义类的构造函数将获取这些服务作为参数.

Your custom class' constructor would then get those services as arguments.

请务必阅读官方文档中的服务容器.它详细介绍了所有这些.

Be sure to read up on the Service Container in the official docs. It goes over all this in great detail.