Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Zit is a simple dependency injector based heavily on Pimple. It aims to provide the same simplicity as Pimple while offering a slightly more robust object interface.
PHP
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib/Zit
tests
.gitignore
.travis.yml
LICENSE.txt
Readme.markdown
composer.json

Readme.markdown

Zit

Build Status

Zit is a simple dependency injector based heavily on Pimple. It aims to provide the same simplicity as Pimple while offering a slightly more robust object interface.

Usage

Zit is simple to use. Just include it and create a new container:

<?php
require_once '/path/to/lib/Zit/Container.php';
$container = new \Zit\Container();
?>

Defining Objects

Like Pimple, objects are defined through anonymous functions that return an instance of the object:

<?php
$container->set('auth', function() {
    return new Auth();
});
?>

All instantiation functions are passed the container as the first argument, making dependency injection possible:

<?php
$container->set('auth', function($container) {
    return new Auth($container->get('db'));
});
?>

Zit also provides convenient magic methods for setting instantiation functions:

<?php
$container->setAuth(function() { // ... }); // Or:
$container->set_auth(function() { // ... });
?>

Getting Objects

Getting objects are as simple as:

<?php
$container->get('auth');
?>

Or, if you prefer the shorthand:

<?php
$container->getAuth(); // Or:
$container->get_auth();
?>

Getting Fresh Objects

By default, all objects are shared in Zit. That is, once an object is created, that same exact object is returned for each additional get(). If you need a fresh object, you can do so with:

<?php
$container->fresh('auth'); // Or:
$container->freshAuth(); // Or:
$container->fresh_auth(); // Or:
$container->newAuth(); // Or:
$container->new_auth();
?>

Note the because the 'new' keyword is reserved, you can only use it if you're using the magic methods.

Constructor Parameters

Sometimes you need to pass parameters to the constructor of an object, while still also injecting dependencies. Zit automatically passes all parameters on to your instantiation function:

<?php
$container->setUser(function($c, $id)) {
    $user = new User($id);
    $user->setDb($c->getDb());
    return $user;
});

$user = $container->newUser(1);
?>

Storing Non-Generators

You can also use Zit to store non-generators (strings/instantiated objects/etc). Just pass it instead of a generator:

<?php
$container->set('api_key', 'abcd1234567890');
$key = $container->get('api_key');
?>

Please note: You must wrap closures with a generator, if you want to closure returned rather than the return value of the closure.

Custom Container

Most projects will benefit from a custom container that sets up its own injection rules. This is as simple as extending Zit:

<?php
namespace MyApp\Di;

class Container extends \Zit\Container
{
    public function __construct()
    {
        $this->setAuth(function() { // ... });
        $this->setUser(function() { // ... });
    }
}
?>

Change Log

Version 2.0

  • Removed the setParam method in favor of checking whether the parameter passed to set is callable.
  • Added the "Factory" variant. This could cause backwards compatibility issues if you have set up objects that end with the word "factory".
  • Updated the delete method so that it clears out objects, callbacks, and factories, which could have some abnormal BC issues as well.
Something went wrong with that request. Please try again.