Yitz Willroth добавил атрибут
Give, позволяющий использовать контекстные зависимости с помощью атрибутов PHP 8:// Традиционный подход - в ServiceProvider
$this->app->when(UserController::class)
->needs(UserRepositoryInterface::class)
->give(DatabaseUserRepository::class);
use Illuminate\Container\Attribute\Give;
// Использование атрибута Give
class UserController extends Controller
{
public function __construct(
#[Give(DatabaseUserRepository::class)]
private UserRepositoryInterface $userRepository
) {}
}
// Ещё один пример
class OrderService
{
public function __construct(
#[Give(StripePaymentProcessor::class)]
private PaymentProcessorInterface $processor
) {}
}
👩💻 Подробности см. в Pull Request #55904.
📱 @dev_notes_ru
#Laravel #Laravel12