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
di
and service definitions are under thedi.service
node.
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
before
orafter
section into thecache
service definition in the form of[callableOrMethodName, OptionalArgumentArray]
, these methods will be executed right before/aftercache
instantiation.callableOrMethodName
here 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).
OptionalArgumentArray
here 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
get
andhas
are 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
*AwareInterface
for your own classes' dependency injection.Container
has all the common injection predefined in thedi.after
section -
Both
ContainerAWareInterface
andContainerAWareTrait
available.
APIs
-
-
get(string $id): object
from ContainerInterface has(string $id): bool
from ContainerInterface
$id
may have@
or@scope
appended. -
Testing
Dependencies
-
PHP >= 7.2.0
- phoole/config >= 1.*