Download the PHP package xp-forge/inject without Composer
On this page you can find all versions of the php package xp-forge/inject. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download xp-forge/inject
More information about xp-forge/inject
Files in xp-forge/inject
Package inject
Short Description Dependency injection for the XP Framework
License BSD-3-Clause
Homepage http://xp-framework.net/
Informations about the package inject
Inject
The inject package contains the XP framework's dependency injection API. Its entry point class is the "Injector".
Binding
Values can be bound to the injector by using its bind()
method. It accepts the type to bind to, an optional name and these different scenarios:
- Binding a class: The typical usecase, where we bind an interface to its concrete implementation.
- Binding an instance: By binding a type to an existing instance, we can create a singleton model.
- Binding a provider: If we need more complicated code to create an instance, we can bind to a provider.
- Binding a named lookup: If we want control over the binding lookups for a type, we can bind to a
Named
instance.
Instance creation
Keep in mind: "injector.get() is the new 'new'". To create objects and perform injection, use the Injector's get() method instead of using the new
keyword or factories.
Manual calls are usually not necessary though, instead you'll use injection:
Injection
Injection is performed by looking at a type's constructor. Bound values will be passed according to the given type hint.
You can supply the type by using parameter attributes in case where the PHP type system is not concise enough. If the bound value's name differs from the parameter name, you can supply a name argument.
When a required parameter is encountered and there is no bound value for this parameter, an inject.ProvisionException
is raised.
Method and field injection are not supported.
Configuration
As seen above, bindings can be used instead of manually performing the wiring. You might want to configure some of your app's settings externally instead of harcoding them. Use the inject.ConfiguredBindings
class for this:
The syntax for these INI files is simple:
Providers
Providers allow implementing lazy-loading semantics. Every type bound to the injector can also be retrieved by a provider. Invoking its get() method will instantiate it.
Named lookups
If we need control over the lookup, we can bind instances of Named
:
All versions of inject with dependencies
xp-framework/reflection Version ^3.0 | ^2.0
php Version >=7.4.0