Tiny dependency management library for PHP

v1.0.1 2016-06-21 05:21 UTC



Build Status Total Downloads Latest Stable Version


Zit is a tiny dependency injection library inspired by Pimple and written by Selvin Ortiz



composer require selvinortiz/zit


sh spec.sh


Dependency management is a difficult topic to explain but essentially, Zit lets you stuff things into it that you can pop out later throughout your application.

// Make an instance
$app = Zit::make();

// Stash a config object
$app->stash('config', new Config(['usr' => 'root', 'pwd' => 'secret']));

// Bind a session generator
$app->bind('session', function() {
    return new Session();

// Bind a database generator
$app->bind('db', function($app) {
    return new Db($app->config->usr, $app->config->pwd);

// Extend your $app with new functionality
$app->extend('end', function($app) {

// Finish

You can also use Zit without making a new instance, the static way if you will.

// Stash a config object
Zit::stash('config', new Config(['usr' => 'root', 'pwd' => 'secret']));

// Bind a session generator
Zit::bind('session', function() {
    return new Session();

// Bind a database generator
Zit::bind('db', function($zit) {
    return new Db($zit->config->usr, $zit->config->pwd);

// Extend Zit with new functionality
Zit::extend('end', function($zit) {

// Finish

Which approach should you use? Whatever you prefer. If you aren't sure, you can use the static way unless you need to create your own instance that extends Zit


Whether you bind(), stash(), or extend() it. You can pop it out using:

Zit::pop('db');  // Formal
Zit::db();       // Via __callStatic()
Zit::db          // Via __callStatic() property sniffing

// If you had done $app = Zit::make()
$app->pop('db'); // Formal
$app->db();      // Via __call()
$app->db;        // Via __get()


pop($id, $args = array())

pop() lets you pop a dependency out of the container

// See the note above on popping stuff out

bind($id, callable $serviceGenerator)

bind() lets you register a service generator. Useful when you need to instantiate a service that depends on a config object for example. Services get generated only once on first call.

Zit::make()->bind('db', function($zit) {
   return new Db($zit->config->usr, $zit->config->pwd); 

stash($id, $service)

stash() lets you stash anything inside the container. You get out what you put in, which is useful if you need to stash a bunch of stuff that needs to be accessible throughout your whole application.

Zit::make()->stash('session', new Session())

extend($id, $callback)

extend() gives you the ability to add new functionality to the container

Zit::make()->extend('logout', function($zit) {


Zit wants to be friendly to first time contributors. Just follow the steps below and if you have questions along the way, please reach out.

  1. Fork it!
  2. Create your bugfix or feature branch
  3. Commit and push your changes
  4. Submit a pull request


Zit is open source software licensed under the MIT License