PHP code example of ice-cream / di

1. Go to this page and download the library: Download ice-cream/di 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/ */

    

ice-cream / di example snippets


use IceCreamDI\Container;

$container = new Container();

$container['service'] = function($c) {
  return new Service();
}

$container['services'] // Returns instance of Service class.

$container = new Container([
  'app.service' => function ($c) {
    return new Service();
  },
]);

> $container = new Container();
> $container['service'] = function ($c) { return new Service(); };
>
> $service = $container['service'];
>
> $container->extend('service', function($service){ $service->someMethodCall(); });
>

use IceCreamDI\Container;

$container = Container();

$container['service'] = function($c) {
  return new Service();
}

$container->extend('service', function($service){
  $service-> ....

  ....

  return $service;
});

use IceCreamDI\Container;

$container = Container();

$container['service'] = function($c) {
  return new Service();
}

$container->raw('service'); // => closure instance.

use IceCreamDI\Container;

$container = Container();

$container['service'] = $container->factory(function($c) {
  return new $service();
});

$container['service']; // => Will be a new instance every time.

use IceCreamDI\Container;

$container = Container();

$container['service_deps'] = [
  'dep1' => ...,
  ...
];

// Notice how you even have access to the containe object $c.
$container['service'] = $container->factory(function($dep1, $dep2, ..., $c) {
  return new $service($dep1, $dep2, ...);
});

$container->resolveFactory('service', 'service_deps'); // Gives you a new service instance with deps passed in.

use IceCreamDI\Container;

$container = Container();

$container['service'] = function() { return new Service(); };

$container->call('service', 'process'); // Same as doing: $container['service']->process();