How can I better understand the level of service in symfony2 in php

I am learning to use symfony2, but I find it difficult to find out what the service level is and where it can be used.

How to better understand the level of service in symfony2

+5
source share
2 answers

I will try my best to explain the services, but Symfony2 docs does a better job than I can.

It has a core, a service is just a class. A class becomes a service when it is registered with Symfony2 Dependency Injection Container(or simply Containerfor brevity.) At this point, the class is part of the application service level.

( ), . . :

  • , , EmailSender
  • , , SMTP, SmtpTransport
  • EmailSender SmtpTransport. , EmailSender , SmtpTransport . (, EmailSender , SmtpTransport .)

, EmailSender . :

$emailSender = new EmailSender(new SmtpTransport());  
$emailSender->send($email)

( EmailSender ), , , SMTP EmailSender sendmail? , .

EmailSender .
:

// YourApp/YourBundle/Resources/config/services.yml
services:
  smtp_transport:
    class: YourApp\YourBundle\Email\SmtpTransport

  email_sender:
    class: YourApp\YourBundle\Email\EmailSender
    arguments:
      - @smtp_transport

, ( Container):

$container->get('email_sender')->send($email);

, ? , , .

, EmailSender , . , , (, ).

, , : a) ; b) "" . Dependency Injection Container, .

, , , Symfony2 PHP . , PHP . , , , . , Symfony2 PHP - .

+8

Fabien : .

+1

All Articles