Bind different implementations of an interface based on the class that needs it
laravelphp
Laravel 12 - Service Container Contextual Binding
<?php
// In app/Providers/AppServiceProvider.php
public function register(): void
{
// Contextual binding - different implementations for different classes
$this->app->when(PhotoController::class)
->needs(StorageInterface::class)
->give(S3Storage::class);
$this->app->when(VideoController::class)
->needs(StorageInterface::class)
->give(LocalStorage::class);
// Tagged bindings for multiple implementations
$this->app->tag([S3Storage::class, LocalStorage::class], 'storage');
// Resolve all tagged services
$storageServices = $this->app->tagged('storage');
}