PHPackages                             gears/di - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. gears/di

AbandonedArchivedLibrary[PSR &amp; Standards](/categories/psr-standards)

gears/di
========

The gears dependency injection container.

v0.2.1(11y ago)1116.1k↓37.5%11MITPHP

Since Oct 2Pushed 11y ago1 watchersCompare

[ Source](https://github.com/phpgearbox/di)[ Packagist](https://packagist.org/packages/gears/di)[ Docs](https://github.com/phpgearbox/di)[ RSS](/packages/gears-di/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (4)Used By (11)

The Di Gear
===========

[](#the-di-gear)

[![Build Status](https://camo.githubusercontent.com/c71ff89fa17738385dfb9d02f8de3c02db0d325aa64eb76f83d53ad43b8ebbb0/68747470733a2f2f7472617669732d63692e6f72672f70687067656172626f782f64692e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/phpgearbox/di)[![Latest Stable Version](https://camo.githubusercontent.com/3525f67ce1fa93f5ebb2f5f4883a1bff7acd95043100f0f45587c3859c7c05d0/68747470733a2f2f706f7365722e707567782e6f72672f67656172732f64692f762f737461626c652e737667)](https://packagist.org/packages/gears/di)[![Total Downloads](https://camo.githubusercontent.com/b3e670dc4a0f6d559485e2eb5c959e5c7199f82e18d23615fb2d70c65337062e/68747470733a2f2f706f7365722e707567782e6f72672f67656172732f64692f646f776e6c6f6164732e737667)](https://packagist.org/packages/gears/di)[![License](https://camo.githubusercontent.com/cda1d7d44737f3200d54817d221ef55f7890f86580cb9ff326c62ac7e7baf884/68747470733a2f2f706f7365722e707567782e6f72672f67656172732f64692f6c6963656e73652e737667)](https://packagist.org/packages/gears/di)

So we have all heard of this thing called Dependency Injection. Its been around for ages in other enterprise enviroments like Java, .Net, etc. But it's sort of new to the PHP world.

Most will understand the general concept but if not check out:

- [http://www.phptherightway.com/#dependency\_injection](http://www.phptherightway.com/#dependency_injection)

How to Install
--------------

[](#how-to-install)

Installation via composer is easy:

```
composer require gears/di:*

```

How to Use
----------

[](#how-to-use)

Firstly this container is like *[Pimple](http://pimple.sensiolabs.org/)*but it is not *Pimple*. There are some unique differences so pay attention.

**Basic example:**

```
// lets import the class
use Gears\Di\Container;

// create a new container object
$container = new Container();

// define some services
$container['session_storage'] = function ()
{
	return new SessionStorage('SESSION_ID');
};

$container['session'] = function ()
{
	return new Session($this['session_storage']);
};

// get the session object
$session = $container['session'];

// define factory service
$container['session_factory'] = $container->factory(function()
{
	return new Session($this['session_storage']);
});

// define container parameters / attributes
$container['cookie_name'] = 'SESSION_ID';

// protecting parameters
$container['random_func'] = $container->protect(function()
{
	return rand();
});
```

> For someone that has previously used the Pimple container you should note that instead of the container being passed in via a function parameter. It is available via the `$this` variable. This is because we bind the closure to the container.

**Using a Service Provider:**

```
use Gears\Di\Container;
use Gears\Di\ServiceProviderInterface;

class FooProvider implements ServiceProviderInterface
{
	public function register(Container $c)
	{
		$c['FooService'] = function(){ return new Foo(); };
	}
}

$container = new Container();
$container->register(new FooProvider());
$container['FooService']->bar();
```

**Object Syntax:**

```
// you can also use the container like this.
$container = new Container();

$container->session_storage = function ()
{
	return new SessionStorage('SESSION_ID');
};

$container->session = function ()
{
	return new Session($this->session_storage);
};

$session = $container->session;
```

**Extending the Container:**

```
class Mail extends Container
{
	// note how we prefix the word inject.
	// this tells us that the property is injectable
	protected $injectTo;

	// private properties however will always be
	// private and can not be injected directly.
	private $sendMailPath;

	// so if you tried to inject fooBar it will fail
	private $injectFooBar;

	// from a naming standpoint I think it is best if you name the injectable
	// properties such that it tells you the type that should be injected.
	// however this isn't enforced.
	protected $injectMessage;

	protected $injectTransportService;

	// this is where we can define default services for our container.
	protected function setDefaults()
	{
		// notice how we set them without the word inject
		$this->to = 'brad@bjc.id.au';

		// I could have defined this above directly on the property
		// but I would rather keep everything consistent.
		$this->sendMailPath = '/bin/sendmail';

		$this->message = function()
		{
			return new Message('Hello World');
		};

		// take note of the camel case property name vs the definition above.
		$this->transportService = function()
		{
			return new SendMailTransport($this->sendMailPath);
		};

		// you can use factory and protect too
		// note you don't have to explicitly define a class property.
		// but just note that both abc and xyz are public properties.
		$this->abc = $this->factory(function(){ return new Abc(); });
		$this->xyz = $this->protect(function($a,$b){ return $a+$b; });
	}

	public function send()
	{
		$this->message->setTo($this->to);
		return $this->transportService->send($this->message);
	}
}

$mail = new Mail();
$mail->send(); // sends an email to me saying Hello World

$mail = new Mail();
$mail->to = 'foo@example.com';
$mail->ip = '127.0.0.1';
$mail->message = function(){ return new Message('bar'); };
$mail->transportService = function(){ return new SmtpTransport($this->ip); };
$mail->send(); // sends an email to foo@example.com via 127.0.0.1 saying bar

// the above could be re written as
$mail = new Mail
([
	'to' => 'foo@example.com',
	'ip' => '127.0.0.1',
	'message' => function(){ return new Message('bar'); },
	'transportService' => function(){ return new SmtpTransport($this->ip); },
]);
$mail->send();
```

Credits
-------

[](#credits)

This is definitely inspired by Fabien's Pimple Di Container.

---

Developed by Brad Jones -

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity31

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~25 days

Total

3

Last Release

4197d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2b476564958ffc91db5580080738e529644f820bdfec6a6c4d397f9c5da45065?d=identicon)[brad-jones](/maintainers/brad-jones)

---

Top Contributors

[![brad-jones](https://avatars.githubusercontent.com/u/2754772?v=4)](https://github.com/brad-jones "brad-jones (4 commits)")

---

Tags

containerdependencyinjectiondiioc

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/gears-di/health.svg)

```
[![Health](https://phpackages.com/badges/gears-di/health.svg)](https://phpackages.com/packages/gears-di)
```

###  Alternatives

[league/container

A fast and intuitive dependency injection container.

86787.8M343](/packages/league-container)[capsule/di

A PSR-11 compliant autowiring dependency injection container.

2857.5k2](/packages/capsule-di)[miladrahimi/phpcontainer

Dependency injection (IoC) container for PHP projects

1322.7k2](/packages/miladrahimi-phpcontainer)[slince/di

A flexible dependency injection container

20260.4k6](/packages/slince-di)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
