PHP code example of irfantoor / container

1. Go to this page and download the library: Download irfantoor/container library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

irfantoor / container example snippets




use IrfanTOOR\Container;

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

use IrfanTOOR\Container;

$container = new IrfanTOOR\Container();

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

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

# defining multiple
$container->set([
  'hello'     => 'world',
  'box'       => 'empty',
  'something' => null,
  'array'     => [
    'action'       => 'go',
    'step'         => 1,
  ],
]);

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

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

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

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

$hash = $container['hash'];

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');
}

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'];
}

# using method remove
$container->remove('hash');

# using unset
unset($container['hash']);

$array = $container->toArray();

$services = $container->keys();

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

foreach($container->toArray() as $k => $v)
{
    $v->close();
}

foreach($container as $k => $v)
{
    $v->close();
}