Download the PHP package modethirteen/opencontainer without Composer
On this page you can find all versions of the php package modethirteen/opencontainer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download modethirteen/opencontainer
More information about modethirteen/opencontainer
Files in modethirteen/opencontainer
Package opencontainer
Short Description A dependency injection container for PHP, please enjoy responsibly
License Apache-2.0
Informations about the package opencontainer
OpenContainer
A dependency injection container for PHP, please enjoy responsibly.
About
OpenContainer was created as an attempt to leverage strict type checking available in full-featured PHP development environments, such as JetBrains PHPStorm, or the native strict type checking of PHP 7+, when managing dependencies from a centralized container. In addition, it contains some experiments with reflection and proxies in order to avoid problems when circular dependencies are introduced in the container's dependency chain.
In addition to exposing dependencies as type-checked class properties, OpenContainer is also compatible with the PSR-11 Container Interface, for interoperability with several PHP frameworks.
Requirements
- PHP 7.4 (main, 2.x)
Installation
Use Composer. There are two ways to add OpenContainer to your project.
From the composer CLI:
Or add modethirteen/opencontainer to your project's composer.json:
dev-main
is the main development branch. If you are using OpenContainer in a production environment, it is advised that you use a stable release.
Assuming you have setup Composer's autoloader, OpenContainer can be found in the modethirteen\OpenContainer\
namespace.
Adding OpenContainer to your application
Simply instantiate OpenContainer and you're ready to go.
Injectable Class
An injectable class is instantiated by injecting the container at construction time. In this example, Foo, Bar, and Baz are all types registered in the container. If a registered type in the container requires Baz's method doSomething(), Baz must first pull it's dependencies, Foo and Bar, from the container (and furthermore, their dependencies, creating a dependency tree).
Registering Types
Registering a type requires a symbol to identify the type when fetching it's instantiated instance from the container, and the fully qualified class name to build.
Registering Instances
Registering an instance requires a symbol to identify the instance when fetching from the container, and the already-created instance itself. Registering an instance is useful when the type's constructor cannot meet the requirements of an injectable class.
Registering Builders
Registering a builder requires a symbol to identify the instance when fetching it from the container, and a closure function to execute the first time it is fetched. Registering a builder is useful if there are specialized steps that must be taken before the instance is created.
Deferred Container
A deferred container attempts to use reflection and class proxies to avoid circular dependencies. A deferred container returns proxies, which are not materialized until a method or property is accessed on the proxy. This behavior is useful as without it, every dependency in the tree is instantiated when the root dependency is first instantiated (whether those downstream dependencies will be eventually used or not). Without deferring dependency instantiation until those dependencies are actually needed, any circular dependency returns a null value, an endless nested function loop, or raises an Undefined Property warning if they can't be resolved.