Quick Answer
DI container autowires dependencies automatically - declare what you need in constructor and the container resolves it. Service locator lets you pull dependencies yourself by name from a global registry. DI container is preferred: dependencies are explicit, testable, and the object does not know about the container. Service locator hides dependencies, makes code harder to test, and is considered an anti-pattern.
Answer
DI containers inject dependencies automatically. Service locator hides dependencies and reduces testability.
S
SugharaIQ Editorial Team
Verified Answer
This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.
PHP interview questions and answers for beginner, junior, mid-level, senior, and expert developers. Ideal for freshers and experienced candidates preparing for PHP roles.