Laravel

Service Container & DI

Dependency injection, service providers, binding, singletons, contextual binding, container resolution, facades

20 คำถามสัมภาษณ์·
Mid-Level
1

What is the Service Container in Laravel?

คำตอบ

The Service Container is a powerful tool for managing class dependencies and performing dependency injection. It automatically resolves dependencies by inspecting constructor type-hints. Using the Service Container improves testability and decoupling by avoiding direct instantiation of dependencies.

2

What is the main advantage of dependency injection compared to direct instantiation?

คำตอบ

Dependency injection improves testability by allowing easy substitution of dependencies with mocks or stubs during testing. It also promotes decoupling by preventing classes from being tightly coupled to their concrete dependencies. This makes code more maintainable and flexible to changes.

3

How to inject a dependency into a Laravel controller constructor?

คำตอบ

Adding a type-hint for the dependency in the constructor allows the Service Container to automatically resolve this dependency. Laravel inspects the constructor parameters and injects the appropriate instances without additional configuration. This approach is the simplest and most common for dependency injection in controllers.

4

What is the difference between bind() and singleton() in the Service Container?

5

What does the app()->make(UserRepository::class) method do?

+17 คำถามสัมภาษณ์

เชี่ยวชาญ Laravel สำหรับการสัมภาษณ์ครั้งถัดไป

เข้าถึงคำถามทั้งหมด flashcards แบบทดสอบเทคนิค แบบฝึกหัด code review และตัวจำลองสัมภาษณ์

เริ่มใช้ฟรี