Irfan's Container: An abstraction for many types of containers with a single API.

1.3.1 2022-11-15 14:40 UTC

This package is auto-updated.

Last update: 2024-06-15 20:54:26 UTC



Note: this Readme will be updated shortly, it reflects the functionality of v1.0.4


Container implementing Psr\ContainerInterface, ArrayAccess, Countable and IteratorAggregate.

The identifiers can use dot notation to access an identifier down a hierarchical level, e.g. to access $container['environment']['headers']['host'] you can code in doted notation as: $config['']


You can initialize by passing an array of key value pairs while creating a new instance, it will use array in memory for this case.


use IrfanTOOR\Container;

$container = new IrfanTOOR\Container(
  # id => values
  'app' => [
    'name'    => 'My App'
    'version' => '1.1',


You can by set a service in the Container by using the method 'set':

use IrfanTOOR\Container;

$container = new IrfanTOOR\Container();

# setting hello => world
$container->set('hello', 'world');

# using an array notation
$container->set(['hello' => 'world']);

# defining multiple
  'hello'     => 'world',
  'box'       => 'empty',
  'something' => null,
  'array'     => [
    'action'       => 'go',
    'step'         => 1,

# defining sub values
$container->set('', 'Another App');
$container->set('debug.level', 2);

# defining a factory service
$container->factory('hash', function(){
    $salt = rand(0, 10000);
    return md5($salt . time());

using array access mechanism:

$container['hello'] = 'world';
$container['debug.log.file'] = '/my/debug/log/file.log';


You can get the stored value or the result from a service in the container by its identifier using the method 'get':

# returns a random hash
$hash = $container->get('hash');

you can also use the array access:

$hash = $container['hash'];

Checking if a value is present in the container

You can use the method 'has' to check if the container has an entry identified with the identifier id:

if ($container->has('hash')) {
  # this will be executed even if the given identifier has the value NULL, 0
  # or false
  echo 'random hash : ' . $container->get('hash');

using the array access the above code will become:

if (isset($container['hash']) {
  # this will be executed even if the given identifier has the value NULL, 0
  # or false
  echo 'random hash : ' . $container['hash'];

Removing an entry

You can use the method 'remove' or unset on the element:

# using method remove

# using unset

Container to Array

The method 'toArray' can be used to convert the container into an array:

$array = $container->toArray();

Array of service identifiers

The array of services identifiers can be retrieved by using the method 'keys':

$services = $container->keys();


The number of items present in the container can be retrieved using the method 'count'. Note that it will return the count of the items at base level.

# will return 1 for the Collection defined in initialization section
$count = $container->count();


The container can directly be used in a foreach loop or wherever an iterator is used. for example the code:

foreach($container->toArray() as $k => $v)

can be simplified as:

foreach($container as $k => $v)