Download the PHP package miladrahimi/phpcontainer without Composer
On this page you can find all versions of the php package miladrahimi/phpcontainer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download miladrahimi/phpcontainer
More information about miladrahimi/phpcontainer
Files in miladrahimi/phpcontainer
Package phpcontainer
Short Description Dependency injection (IoC) container for PHP projects
License MIT
Homepage https://github.io/miladrahimi/phpcontainer
Informations about the package phpcontainer
PhpContainer
A dependency injection (Inversion of Control) container written in PHP programming language, compliant with PSR-11 standards.
Features:
- Singleton, transient, and closure bindings
- Explicit and implicit bindings
- Typed and named bindings
- Automatic injection
Overview
Dependency Inversion is a fundamental concept in Object-oriented design.
It leads to important ideas like Dependency Injection, Inversion of Control, and the creation of an IoC Container.
For PHP projects, there's the PhpContainer, a handy tool that provides a dependency injection container (IoC Container) conforming to PSR-11 standards.
Installation
To integrate PhpContainer into your project, use the following Composer command:
Documentation
Explicit Binding
Explicit binding involves directly linking an abstraction to a concrete implementation.
This binding can be achieved by using the singleton()
, transient()
, and closure()
methods.
Binding Methods
- Singleton binding: The container creates the concrete only once and returns it whenever needed.
- Transient binding: The container clones or creates brand-new concrete each time you need it.
- Closure binding: Only for closures. It prevents the container from calling the closure (the default behavior).
The following example demonstrates the differences between singleton and transient binding.
Implicit Binding
When the container needs a class without a specific binding, it tries to create an instance.
In the example below, it instantiates the MySQL
class in the provided code.
But if it encounters an abstract class or an interface that can't be instantiated directly, an error occurs.
Binding to Objects
You can connect abstracts to specific objects. Using singleton binding gives you the original object when required, while transient binding offers a fresh copy of the object every time you ask for it.
Constructor Auto-injection
Concrete classes might contain constructor parameters that either possess default values or can be resolved by the container.
Binding Using Closure
The following example illustrates how to bind using Closure.
In singleton binding, the container executes the Closure once and retrieves the result whenever needed.
Conversely, in transient binding, the container invokes the Closure each time it's required.
If you intend to bind an abstraction to a Closure without immediate invocation by the container, you can use the closure()
method instead.
Resolving Using Closure
You have the option to use the call
method, allowing the container to execute the provided function or closure and resolve its arguments.
Type-based and name-based binding
PhpContainer supports typed-based and name-based bindings. The following example demonstrates these types of bindings.
Error handling
The ContainerException
might be raised by the container for several reasons.
It can arise when a ReflectionException
occurs, indicating a missing concrete implementation for a provided abstraction.
Additionally, this exception occures when the container cannot inject parameter values into concrete constructors or closures.
License
PhpContainer is created by Milad Rahimi and released under the MIT License.