PHPackages                             terrydjony/ion - 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. terrydjony/ion

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

terrydjony/ion
==============

Ion is a minimalist inversion of control container using automatic dependency injection

v1.0(9y ago)018MITPHPPHP &gt;=5.3.0

Since Mar 26Pushed 9y ago1 watchersCompare

[ Source](https://github.com/terryds/ion)[ Packagist](https://packagist.org/packages/terrydjony/ion)[ RSS](/packages/terrydjony-ion/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependenciesVersions (3)Used By (0)

ION Container
=============

[](#ion-container)

A minimalist inversion of control container using automatic dependency injection

Installation
------------

[](#installation)

To install **ION Container**, you need to add it to your composer.json file

```
{
      "require": "terrydjony/ion"
}

```

Or, by using terminal

```
composer require terrydjony/ion

```

Usage
-----

[](#usage)

First, you need to instantiate the container

```
use Ion\Container;

$ion = new Container();
```

Instantiating Object
--------------------

[](#instantiating-object)

To instantiate any objects, you should use `make($class, array $args = array())`, instead of `new` keyword

Notice that the parameter two `$args` is optional For example, if you want to instantiate `stdClass` class.

```
$stdClass = $ion->make('stdClass');

// $stdClass is an instance of stdClass
```

Registering Service
-------------------

[](#registering-service)

You can register the way to instantiate some class via `register($key, $closure)`Here is an example of registering PDO class with some defined arguments

```
$ion->register('PDO', function() {
    return new PDO('mysql:host=localhost;dbname=databasename','root','');
};

$pdo = $ion->make('PDO');

// $pdo is an instance of PDO class with the arguments as defined before
```

The above example will register PDO service. So, if you instantiate it via `$ion->make('PDO')`, you don't need to supply any arguments since all the arguments have been defined before.

However, it will be nice to have a database config.

```
$config = array('dsn'=>'mysql:host=localhost;dbname=databasename','username'=>'root','password'=>'');

$ion->register('PDO', function() use ($config) {
    return new PDO($config['dsn'],$config['username'],$username['password']);
});

$pdo = $ion->make('PDO');
And, notice that the first parameter of register is a key. The key can be anything, but it's recommended to use the class name as the key.

$ion->register('DAL', function() {
    return new PDO($config['dsn'],$config['username'],$config['password']);
};

$dal = $ion->make('DAL');
// $dal is a PDO object
```

Notice that if you register a service, it'll be a shared instance. If you want to register a factory, use registerFactory

Registering Factory Services
----------------------------

[](#registering-factory-services)

You can register a factory using `registerFactory($key, $closure)`

```
$ion->registerFactory('CarFactory', function() {
    return new Car();
};

$car1 = $ion->make('CarFactory');
$car2 = $ion->make('CarFactory');

// $car1 and $car2 are different objects
If you want to use the supplied arguments, you can use getArgs() method.

$this->ion->registerFactory('User', function($ion) {
  return new User($ion->getArgs());
});

$user1 = $this->ion->make('User',array('Foo'));
// $user1 is an instance with argument Foo
```

Registering Parameters
----------------------

[](#registering-parameters)

You can also register parameters using `setParam($param, $value)` and get the value via `param($param_name)`

```
$ion->setParam('db_dsn','mysql:host=localhost;dbname=databasename');
$ion->setParam('db_user','root');
$ion->setParam('db_password','');

$ion->register('PDO', function($ion) {
    return new PDO($ion->param('db_dsn'), $ion->param('db_user'), $ion->param('db_pass');
}
```

Forcing New Instance
--------------------

[](#forcing-new-instance)

You can force a new instance using `makeNew($key, $args=array())`.

```
// register a PDO service before

$pdo1 = $ion->makeNew('PDO');
$pdo2 = $ion->makeNew('PDO');

// $pdo1 and $pdo2 are different objects no matter how the PDO service is registered
```

Defining Default Class For Interface
------------------------------------

[](#defining-default-class-for-interface)

You can bind an interface to a class. So, if a class depends on that interface, that class will represent that interface

If the class must be instantiated with some arguments, you should register it first. Then, you give the key as the parameter two of bindInterface

But, if the class can be instantiated with no arguments, you can just specify the class name.

```
class A
{
    public function __construct(DatabaseAdapterInterface $dba, StandardClassInterface $stdClass, StandardInterface $obj)
    {

    }
}

$ion->register('PDO', function() {
    return new PDO('mysql:host=localhost;dbname=databasename','root','');
}

// Binds DatabaseAdapterInterface to PDO class which has been registered
$ion->bindInterface('DatabaseAdapterInterface', 'PDO');

// Binds StandardClassInterface to stdClass which needs no arguments for construction
$ion->bindInterface('StandardClassInterface', 'stdClass');

$a = $ion->make('A');
```

Automatic Dependency Injection
------------------------------

[](#automatic-dependency-injection)

Suppose you want to instantiate a class A that needs class B which needs class C

I mean something like

```
class A
{
  public function __construct(B $b)
  {

  }
}

class B
{
  public function __construct(C $c)
  {

  }
}

class C
{
  public function __construct()
  {

  }
}
```

And, to make a class A instance, you just need to:

```
$a = $ion->make('A');
```

And, voila! You get the class A instance!

If you have a dependency that needs some parameters, you can register the dependency first before making object that needs that dependency

And, if the class needs some arguments, you can supply an array of arguments as parameter two of make or makeNew method.

```
class A
{
    public function __construct(B $b, $name)
    {
        echo "Hello ".$name
    }
}

class B
{
    public function __construct(DatabaseAdapterInterface $dba)
    {

    }
}

$ion->register('PDO', function() {
    return new PDO('mysql:host=localhost;dbname=databasename','root','');
});
$ion->bindInterface('DatabaseAdapterInterface','PDO');

$a = $ion->make('A',array('World'));
```

The class A instance is made automatically.

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~1 days

Total

2

Last Release

3360d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/227af899f51765827a4a539ba2aa577d5167d46f0170a3bdb0e0f3d0806604e8?d=identicon)[terryds](/maintainers/terryds)

---

Top Contributors

[![gideonaibot](https://avatars.githubusercontent.com/u/263087710?v=4)](https://github.com/gideonaibot "gideonaibot (5 commits)")[![littlepixel123](https://avatars.githubusercontent.com/u/185886589?v=4)](https://github.com/littlepixel123 "littlepixel123 (5 commits)")[![openclaw-instance001](https://avatars.githubusercontent.com/u/263087710?v=4)](https://github.com/openclaw-instance001 "openclaw-instance001 (5 commits)")[![terryds](https://avatars.githubusercontent.com/u/11571395?v=4)](https://github.com/terryds "terryds (3 commits)")

---

Tags

containerdependency-injectiondiioc

### Embed Badge

![Health badge](/badges/terrydjony-ion/health.svg)

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

###  Alternatives

[php-di/php-di

The dependency injection container for humans

2.8k53.2M1.2k](/packages/php-di-php-di)[slince/di

A flexible dependency injection container

20268.4k6](/packages/slince-di)[capsule/di

A PSR-11 compliant autowiring dependency injection container.

2859.2k2](/packages/capsule-di)

PHPackages © 2026

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