implements Phalcon\DiInterface, ArrayAccess
Phalcon\Di is a component that implements Dependency Injection/Service Location of services and it’s itself a container for them.
Since Phalcon is highly decoupled, Phalcon\Di is essential to integrate the different components of the framework. The developer can also use this component to inject dependencies and manage global instances of the different classes used in the application.
Basically, this component implements the Inversion of Control pattern. Applying this, the objects do not receive their dependencies using setters or constructors, but requesting a service dependency injector. This reduces the overall complexity, since there is only one way to get the required dependencies within a component.
Additionally, this pattern increases testability in the code, thus making it less prone to errors.
use Phalcon\Di; use Phalcon\Http\Request; $di = new Di(); // Using a string definition $di->set("request", Request::class, true); // Using an anonymous function $di->setShared( "request", function () { return new Request(); } ); $request = $di->getRequest();
Phalcon\Di constructor
Sets the internal event manager
Returns the internal event manager
Registers a service in the services container
Registers an “always shared” service in the services container
Removes a service in the services container It also removes any shared instance created for the service
Attempts to register a service in the services container Only is successful if a service hasn’t been registered previously with the same name
Sets a service using a raw Phalcon\Di\Service definition
Returns a service definition without resolving
Returns a Phalcon\Di\Service instance
Resolves the service based on its configuration
Resolves a service, the resolved service is stored in the DI, subsequent requests for this service will return the same instance
Check whether the DI contains a service by a name
Check whether the last service obtained via getShared produced a fresh instance or an existing one
Return the services registered in the DI
Check if a service is registered using the array syntax
Allows to register a shared service using the array syntax
$di["request"] = new \Phalcon\Http\Request();
Allows to obtain a shared service using the array syntax
var_dump($di["request"]);
Removes a service from the services container using the array syntax
Magic method to get or set services using setters/getters
Set a default dependency injection container to be obtained into static methods
Return the latest DI created
Resets the internal default DI
© 2011–2017 Phalcon Framework Team
Licensed under the Creative Commons Attribution License 3.0.
https://docs.phalconphp.com/en/latest/api/Phalcon_Di.html