Oihana PHP System

initContainer.php

Table of Contents

Functions

initContainer()  : Container
Initialize and build a PHP-DI container for the application.

Functions

initContainer()

Initialize and build a PHP-DI container for the application.

initContainer(string|array<string|int, mixed>|DefinitionSource ...$definitions) : Container

This function creates a ContainerBuilder, adds one or more definition sources, and returns the built Container. Definitions can be provided as:

  • string: path to a PHP definition file (returning an array of definitions).
  • array: an associative array of definitions.
  • DefinitionSource: any PHP-DI compatible definition source.

Notes:

  • Later definition sources can override entries defined earlier.
  • To load multiple files from a directory, see initDefinitions().
Parameters
$definitions : string|array<string|int, mixed>|DefinitionSource

One or more definition sources (file path(s), array(s), or DefinitionSource instances).

Tags
throws
Exception

If the container build process fails.

see
initDefinitions()
see
ContainerBuilder
link

PHP-DI definitions documentation

example
use DI\Container;
use function oihana\init\{initContainer, initDefinitions};

// From a definitions directory (merges all PHP files returning arrays)
$definitions = initDefinitions(__DIR__ . '/../../definitions');

// Add additional inline definitions that can override previous ones
$inline =
[
    'config.timezone' => 'UTC',
];

// Build the container with multiple sources
// @var Container $container
$container = initContainer($definitions, $inline, __DIR__ . '/extra-definitions.php');

// Retrieve a service
// $logger = $container->get(App\Logger::class);
Return values
Container

The built dependency injection container.


        
On this page

Search results