Container Configuration
On this page
The Norvica DI Container is configured through a PHP file (let’s name it container.php
), and managed using the
Configurator
class. This configuration process defines the services, parameters, and relationships that the container
will manage.
Configuration File
The container.php
file is a standard PHP return array. Each key in the array represents a service ID (a unique
identifier), and the corresponding value defines how the container should create and manage the service.
Structure and Format:
- Service ID: Choose meaningful IDs that reflect the purpose of the service (e.g.,
logger
,mailer
). Can also be a FQCN (fully qualified class name), e.g.Foo\Bar
. - Definition: This can be one of the following:
- A scalar value (string, number, boolean)
- An anonymous function (for dynamic value/service creation)
- A call to a helper function like
obj()
,ref()
, orenv()
to define object services or dependencies. - An array of service definitions (for collections)
Configurator Class
The Configurator
class provides the interface for loading the configuration file, customizing container behavior, and
building the final container instance.
Example Usage:
Explanation:
- A new
Configurator
object is created. - The
load()
method reads the definitions from thecontainer.php
file. - Optionally, you can use
snapshot()
to compile the configuration orautowiring()
to disable autowiring behavior. - Finally,
container()
builds and returns the container object, ready for use.
In the following sections, we’ll dive deeper into the different ways you can define services and parameters within the
container.php
file.