Download the PHP package phoole/di without Composer
On this page you can find all versions of the php package phoole/di. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package di
di
Slim, powerful and full compatible PSR-11 dependency injection library for PHP.
It builds upon the versatile phoole/config library and supports object scope and more. It requires PHP 7.2+. It is compliant with PSR-1, PSR-4, PSR-11 and PSR-12.
Installation
Install via the composer utility.
or add the following lines to your composer.json
Usage
-
With configuration from files or definition array
Container related configurations are under the node
diand service definitions are under thedi.servicenode.
Features
-
References in the form of '${reference}' can be used to refer to predefined parameters from the config or services in the container.
Characters of
'$', '{', '}', '.'are not allowed in reference name. Characters of'#', '@'have special meanings, such that should not be part of normal service names.See phoole/config reference for detail. Parameter references are read from configuration files or array.
Service object reference in the form of
${#serviceId}can be used to referring a service instance in the container.Two reserved service references are
${#container}and${#config}. These two are referring the container instance itself and the config instance it is using. These two can be used just like other service references.- Using references
References can be used anywhere in the configuration.
-
Object decorating is to apply decorating changes (executing methods etc.) right before or after the instantiation of a service instance.
- Decorating methods for individual instance only
By adding
beforeoraftersection into thecacheservice definition in the form of[callableOrMethodName, OptionalArgumentArray], these methods will be executed right before/aftercacheinstantiation.callableOrMethodNamehere can be,-
method name of initiated object
-
a valid callable which takes initiated object as parameter
- a pseudo callable with references (after resolving the references, it is a valid callable).
OptionalArgumentArrayhere can be,-
empty
- array of values or references
- Common decorating methods for all instances
Common methods can be configured in the 'di.before' or 'di.after' node to apply to all the instances right before or after their instantiation.
-
- Shared objects and new objects
By default, service instances in the container are shared inside the container. If users want different instance each time, they may just append '@' to the service id.
- Object scope
You may get an instance in your own scope as follows
Service references can also have scope defined as follows,
-
- Access predefined services statically
Services in the container can also be access through a static way. But
getandhasare reserved.- Initiating object by taking advantage of dependency injection
-
- Parameter autowiring (resolving)
Parameters of a constructor/callable will be resolved by looking
-
exists in the classmap (service objects created already) ?
-
classname known to the script (class defined already) ?
- Auto injection
Instead of using 'annotation', we encourage of using
*AwareInterfacefor your own classes' dependency injection.Containerhas all the common injection predefined in thedi.aftersection -
Both
ContainerAWareInterfaceandContainerAWareTraitavailable.
APIs
-
-
get(string $id): objectfrom ContainerInterface has(string $id): boolfrom ContainerInterface
$idmay have@or@scopeappended. -
Testing
Dependencies
-
PHP >= 7.2.0
- phoole/config >= 1.*
