Dependency Injection and Service Location in PHP

RequirePHP mimics RequireJS's API as closely as possible to allow easy module loading in PHP. At less than 5KB, RequirePHP provides a lot of value in a small package. It's so easy to use, you can be up and running in 5 minutes.

Easy to Integrate

Your app is already ready to integrate with RequirePHP. It's as easy as wrapping your files' include calls with a list of dependencies. Get all the benefits of dependency injection and service locator patterns instantly. All the cool kids are already using dependency injection.

Why Use Dependency Injection

Because dependency injection doesn't require any change in code behavior it can be applied to legacy code as a refactoring. The result is more independent clients that are easier to unit test in isolation using stubs or mock objects that simulate other objects not under test. This ease of testing is often the first benefit noticed when using dependency injection.

Dependency injection allows a client to remove all knowledge of a concrete implementation that it needs to use. This helps isolate the client from the impact of design changes and defects. It promotes reusability, testability and maintainability.

Dependency injection can be used to externalize a system's configuration details into configuration files allowing the system to be reconfigured without recompilation. Separate configurations can be written for different situations that require different implementations of components. This includes, but is not limited to, testing.

Reduction of boilerplate code in the application objects since all work to initialize or set up dependencies is handled by a provider component.

Dependency injection allows concurrent or independent development. Two developers can independently develop classes that use each other, while only needing to know the interface the classes will communicate through. Plugins are often developed by third party shops that never even talk to the developers who created the product that uses the plugins.