12. How to use Pimple
// in bootstrap phase of your codes
use PimpleContainer;
$container = new Container();
// define a service
$container['storage'] = function ($c) {
return new Storage();
};
13. How to use Pimple
// in business logic of your codes
public function indexAction()
{
$storage = $this->$container[ storage ];
…
}
14. Internal of Pimple
class Container implements ArrayAccess
{
protected $values = [];
public function __construct(array $values = [])
{
$this->values = $values;
}
public function offsetSet($key, $value)
{
$this->values[$key] = $value;
}
public function offsetGet($key)
{
$value = $this->values[$key];
return is_callable($value)? $value($this) : $value;
}
public function offsetExists($key)
{
return array_key_exists($key, $this->values);
}
public function offsetUnset($key)
{
unset($this->values[$key]);
}
}