Woodlandtrek
4 years ago
123
6
Laravel

Inject 2 instances of same class in constructor

Posted 4 years ago by Woodlandtrek

Hi,

Here is a situation I ran across recently: If I have 2 different instances of the same class bound into the IoC container, how can I select the correct one when using dependency injection?

Example:

$app(‘foo1’) = new Foo(‘hello’); 
$app(‘foo2’) = new Foo(‘world’); 

How would I inject $app(‘foo2’) into a constructor (assuming both instances have been registered into the container as singletons)?

Thanks

Please sign in or create an account to participate in this conversation.