12.
Configuring Specific (Named) Arguments
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
11 Comments
Hi Ryan,
I was wondering if there are any differences using this:
instead of this:
since it looks like both works.
Yo Bruno Lima!
Wow, nice find! They're both identical actually! But in all of the 3.3 changes, I didn't even know this was one of them! So yea, use whichever you want. The shorter syntax is pretty cool looking :). The only downside is if you ever need to add more config (like tags), you'll then need to put the arguments under an arguments key. But, that's not too common.
Cheers!
Great! Thank you.
Hi Ryan,
I got stucked somewhere when i was trying to do the same in my own code.
In my Container Class I'm passing two services as u can see,
When i'm trying to pass the arguments inside it it throwing an error:
YML CONFIG:
And when i'M trying to give service name in this format:
As u told first search the container and then use that value as a argument:-but when i do debug:container i am unable to find the service.
So i went to appDevContainer:- Here they are using something like this:-
return $this->services['AppBundle\Services\ServiceContainer'] = new \AppBundle\Services\ServiceContainer(new \AppBundle\Services\TwitterClient(), new \AppBundle\Test\Rot13Transformer());
But when i get into your project appDevCotainer File :- they are using services in this format:
And one more thing if i remove all the config from services.yml its working fine. ??
Hey Vivek Sharma
You have a subtle error, look at the service you are injecting to ServiceContainer
You are saying that
$transformerDriveris an instance of ServiceContainer, but `$transformerDriver is a property of ServiceContainerWhen you remove everything, I think it is working because you have type-hinted your arguments, so autowire + autoconfigure can do all the work
Have a nice day
Hi Ryan,
In my services.yml, '@service_container' is marked with a warnng in PhpStorm. The warning is "expect instance of: Symfony\Component\DependancyInjection\Container Provided instance of service id does not match"
Is this warning related to the autowiring? How do you fix it? Thanks.
Hey Scott,
Hm, what typehint do you have for this argument in your service? I wonder about FQCN of this class. Looks like you misprint namespace, it should be "Symfony\Component\DependencyInjection\Container", or probably better use interface instead "Symfony\Component\DependencyInjection\ContainerInterface", notice "Dependency" not "Dependancy".
Cheers!
Sorry, <em>"Dependancy"</em> was a typo. Here is the class:
Here is the entry in services.yml:
Hey Scott,
Hm, looks correct, so you can just ignore it :) I think it can be a bug in PhpStorm inspection. Well, I did a bit of debugging:
bin/console debug:container | grep service_container
And I see that "service_container" service points to Symfony\Component\DependencyInjection\ContainerInterface, so try to use the interface instead in __construct():
Probably it will fix the warning. Actually, better always use interfaces if possible for type-hints.
Cheers!
Wow, that worked! Thank you very much.
For my education, why do you think it is better to use interfaces instead of the actual class?
Hey Scott,
Because interfaces gives you more flexibility! If you use an interface for a type-hint - you can pass *any* object which implements this interface. But if you type-hint with a real object - you are allowed to pass only this object, or any object which extends this object. Actually, it's not too important, but you would see the difference if you have a few classes which implements the same interfaces. For example, let's say we have CacheInterface and a few classes which implements this interface like RedisCache and LocalCache. If you type-hint with CacheInterface - you can easily switch your cache provider and even don't need to change the type-hint.
If you wonder more about the difference between Classes, Abstract classes, Interfaces, Traits, etc. and their use cases - take a look at our https://knpuniversity.com/s... course.
Cheers!
"Houston: no signs of life"
Start the conversation!