Laravel

Service Container & DI

Dependency injection, service providers, binding, singletons, contextual binding, การ resolve container, facades

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

Service Container ใน Laravel คืออะไร?

คำตอบ

Service Container เป็นเครื่องมือทรงพลังสำหรับการจัดการ dependency ของ class และการทำ dependency injection มันจะ resolve dependency โดยอัตโนมัติด้วยการตรวจสอบ type-hint ของ constructor การใช้ Service Container ช่วยเพิ่มความสามารถในการทดสอบและลดการเชื่อมโยงโดยหลีกเลี่ยงการสร้าง instance ของ dependency โดยตรง

2

ข้อได้เปรียบหลักของ dependency injection เมื่อเทียบกับการสร้าง instance โดยตรงคืออะไร?

คำตอบ

Dependency injection ช่วยเพิ่มความสามารถในการทดสอบโดยให้สามารถแทนที่ dependency ด้วย mock หรือ stub ได้ง่ายในระหว่างการทดสอบ นอกจากนี้ยังส่งเสริมการลดการเชื่อมโยงโดยป้องกันไม่ให้ class ผูกติดแน่นกับ dependency ที่เป็นรูปธรรม ทำให้โค้ดดูแลรักษาง่ายและยืดหยุ่นต่อการเปลี่ยนแปลงมากขึ้น

3

จะ inject dependency เข้าไปใน constructor ของ controller Laravel ได้อย่างไร?

คำตอบ

การเพิ่ม type-hint ของ dependency ใน constructor ช่วยให้ Service Container resolve dependency นี้โดยอัตโนมัติ Laravel จะตรวจสอบพารามิเตอร์ของ constructor และ inject instance ที่เหมาะสมโดยไม่ต้องตั้งค่าเพิ่มเติม วิธีนี้เป็นวิธีที่ง่ายและพบบ่อยที่สุดสำหรับ dependency injection ใน controller

4

ความแตกต่างระหว่าง bind() และ singleton() ใน Service Container คืออะไร?

5

เมธอด app()->make(UserRepository::class) ทำอะไร?

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

หัวข้อสัมภาษณ์ Laravel อื่นๆ

พื้นฐาน PHP

Junior
25 คำถาม

พื้นฐาน OOP ใน PHP

Junior
20 คำถาม

Composer & Autoloading

Junior
18 คำถาม

พื้นฐาน Laravel

Junior
20 คำถาม

Laravel Routing

Junior
20 คำถาม

Blade Templates

Junior
18 คำถาม

Request & Response

Junior
20 คำถาม

พื้นฐาน Eloquent ORM

Junior
22 คำถาม

Eloquent Relationships

Mid-Level
25 คำถาม

Migrations & Schema Builder

Mid-Level
20 คำถาม

การตรวจสอบและฟอร์ม

Mid-Level
22 คำถาม

การยืนยันตัวตน

Mid-Level
20 คำถาม

การให้สิทธิ์และ Policies

Mid-Level
18 คำถาม

API Resources & Authentication

Mid-Level
26 คำถาม

Middleware

Mid-Level
18 คำถาม

Queues & Jobs

Mid-Level
22 คำถาม

Events & Listeners

Mid-Level
18 คำถาม

การแจ้งเตือนและเมล

Mid-Level
20 คำถาม

File Storage

Mid-Level
18 คำถาม

Testing & PHPUnit

Mid-Level
24 คำถาม

Caching

Mid-Level
18 คำถาม

Livewire & Inertia

Mid-Level
20 คำถาม

Eloquent Advanced

Senior
24 คำถาม

Repository Pattern

Senior
20 คำถาม

Laravel Packages

Senior
20 คำถาม

Performance Optimization

Senior
22 คำถาม

Security Best Practices

Senior
22 คำถาม

Laravel Octane

Senior
18 คำถาม

Laravel Distributed Systems

Senior
22 คำถาม

Observability & Monitoring

Senior
20 คำถาม

Deployment & DevOps

Senior
20 คำถาม

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

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

เริ่มใช้ฟรี