ArrayAccess for Container
Overview
The DomainFlow Container leverages PHP’s ArrayAccess interface to allow you to manage service bindings using familiar array syntax. This feature provides a concise and intuitive way to register, retrieve, and remove services, making dependency management more flexible and straightforward.
Key Concepts
Intuitive Array Syntax:
Use array-style operations (e.g., $container['Service'] = $instance
) instead of explicit method calls like bind()
or get()
, simplifying your code.
Automatic Registration:
The container automatically determines how to register a value—whether as a shared instance, a factory closure, or a concrete value—based on the type provided.
Graceful Error Handling:
When accessing a non-existent service, the container typically returns null
instead of throwing an exception, ensuring smoother error management.
Implementation Details
The DomainFlow Container implements the following ArrayAccess methods to support this functionality:
Method | Signature | Description |
---|---|---|
offsetExists | offsetExists(mixed $offset): bool | Checks if a binding exists for the given key. |
offsetGet | offsetGet(mixed $offset): mixed | Retrieves the service instance associated with the given key. |
offsetSet | offsetSet(mixed $offset, mixed $value): void | Registers a service binding using array syntax. |
offsetUnset | offsetUnset(mixed $offset): void | Removes the service binding from the container. |
Example Usage
Below is an example demonstrating how to use array access to manage container bindings:
$container = new Container();
// Register a service using a factory closure via array syntax.
$container[FileLogger::class] = function () {
return new FileLogger();
};
// Alternatively, register a shared instance.
$sharedLogger = new FileLogger();
$container['sharedLogger'] = new Shared($sharedLogger);
// Check if a binding exists using array syntax.
if (isset($container[FileLogger::class])) {
echo "FileLogger is registered in the container.\n";
}
// Retrieve a service using array access.
$fileLogger = $container[FileLogger::class];
$fileLogger->log("Logging via array access.");
// Unset a binding.
unset($container[FileLogger::class]);
In this example, the container demonstrates the versatility of array access by allowing you to register services both as closures (for factory bindings) and as pre-instantiated shared objects, as well as check, retrieve, and remove bindings.
Benefits
Concise and Intuitive: Array syntax makes your code cleaner and easier to read, reducing the need for verbose method calls.
Flexibility: Dynamically register and retrieve services with minimal overhead, streamlining dependency management.
Seamless Integration: Leveraging PHP’s built-in ArrayAccess interface ensures that the container feels natural to use, integrating smoothly with standard PHP language features.
Summary
Using ArrayAccess with the DomainFlow Container offers a developer-friendly approach to service management. It simplifies binding registration, retrieval, and removal through familiar array operations, all while handling errors gracefully and automatically managing service instantiation based on the type of value provided.