A simple Dependency Injector for PHP 5.4+

0.3.0 2014-03-28 16:28 UTC


Build Status

A simple dependency injection library

The Zap Injector looks to find the middle ground between the simplicity offered by tiny service locators like Pimple and the real dependency injection libraries like PHP-DI provide.

Why would I use this?

  • It handles resolving dependencies and their configuration in order.
  • It allows for dependencies to only be loaded on demand.
  • It allows configuration to only be loaded on demand, and to be configured from anywhere.
  • If you register under parent classes or interfaces, it decouples modules from their dependencies.


$injector = new \iMarc\Zap\Injector();

// Register a factory under a class or interface name:
$injector->register('Request', function() {
	return new Request();

// Or Register a specific instance:

// Or register a class to simply be constructed:

// Invoke a callable, and Injector will fill in the dependencies:
$returnValue = $injector->invoke(function(Request $req, Session $sess) {
	return array($req, $sess);

// Similarly, construct an instance of a class with dependencies:
$instance = $injector->create('some\class');

Also, Injector has an extend method. Extensions are invoked immediately after a new instance is constructed from a factory or from a classname:

$injector->extend('Request', function(Request $req) {




  • Major refactoring and code cleanup.


  • Added ->register(), removed ->addFactory(), ->addInstance(), and ->addClass()
  • Renamed ->remove() to ->unregister()
  • Other refactoring


  • Added ->extend() method, similar to Pimple. Callbacks configured with ->extend() are called after an instance is created by a factory.


  • Initial Release, full of bugs