diff --git a/components/dependency_injection/tags.rst b/components/dependency_injection/tags.rst index 500aa1bb3e0..1c4e08590e4 100644 --- a/components/dependency_injection/tags.rst +++ b/components/dependency_injection/tags.rst @@ -153,7 +153,7 @@ custom tag:: $taggedServices = $container->findTaggedServiceIds( 'acme_mailer.transport' ); - foreach ($taggedServices as $id => $attributes) { + foreach ($taggedServices as $id => $tags) { $definition->addMethodCall( 'addTransport', array(new Reference($id)) @@ -178,7 +178,7 @@ run when the container is compiled:: use Symfony\Component\DependencyInjection\ContainerBuilder; $container = new ContainerBuilder(); - $container->addCompilerPass(new TransportCompilerPass); + $container->addCompilerPass(new TransportCompilerPass()); .. note:: @@ -291,8 +291,8 @@ use this, update the compiler:: $taggedServices = $container->findTaggedServiceIds( 'acme_mailer.transport' ); - foreach ($taggedServices as $id => $tagAttributes) { - foreach ($tagAttributes as $attributes) { + foreach ($taggedServices as $id => $tags) { + foreach ($tags as $attributes) { $definition->addMethodCall( 'addTransport', array(new Reference($id), $attributes["alias"]) @@ -302,7 +302,7 @@ use this, update the compiler:: } } -The trickiest part is the ``$attributes`` variable. Because you can use the -same tag many times on the same service (e.g. you could theoretically tag -the same service 5 times with the ``acme_mailer.transport`` tag), ``$attributes`` -is an array of the tag information for each tag on that service. +The double loop may be confusing. This is because a service can have more than one +tag. You tag a service twice or more with the ``acme_mailer.transport`` tag. The +second foreach loop iterates over the ``acme_mailer.transport`` tags set for the +current service and gives you the attributes.