phpbench/container
Lightweight, extensible PHP dependency injection container with parameters and service tagging. Register services as closures, load extensions to provide defaults and wiring, and discover tagged services. Implements container-interop for portability.
Start by installing the package via Composer: composer require phpbench/container. The core API is intentionally minimal: instantiate PhpBench\Container\Container, register services via register($id, $factory, $tags = []), and resolve them with get($id). For parameters, use setParameter($name, $value) and getParameter($name). The first real-world use case is typically bootstrapping a small app or library: register core services (e.g., a logger, database client) in register() calls or via an extension class. Check the README’s Simple usage and Extending and Tagging sections first—they cover the most common patterns. Initial setup often involves creating a config class or Extension to centralize registrations.
ExtensionInterface implementations to encapsulate related service definitions and defaults (getDefaultConfig()). Pass them to the container constructor before init().[ 'tag' => ['priority' => 10] ]) and iterate over them using getServiceIdsForTag($tag)—ideal for plugins, middleware, or event subscribers.'foo_bar') but are accessed as fooBar in services via $container->getParameter('foo_bar')—this improves interoperability with YAML/JSON configs.Container::SCOPE_PROTOTYPE as the third argument to register().Psr\Container\ContainerInterface are used.init() must be called explicitly after passing extensions to the constructor—services won’t be registered until then. Forgetting this is a common source of “service not found” errors.['tag' => ['param1' => 'foobar']]) is only available via getServiceIdsForTag($tag) which returns a map of serviceId => metadataArray. Missing metadata defaults to empty array, so validate before use.'my_service') is the key used in get(), register(), and tag lookups—not the class name. Consider using class names as IDs for convention-over-configuration.'foo_bar') but are converted to camelCase internally (fooBar) when accessed via getters—but only when using Container::getParameter(); direct array access on config defaults uses snake_case. This can trip up developers mixing manual config and extension defaults.Container::has($id) to avoid exceptions when checking for optional services before get().How can I help you explore Laravel packages today?