Phalcon di
NOTE
All classes are prefixed with Phalcon
Di¶
-
Namespace
Phalcon
-
Uses
Phalcon\Config\Adapter\Php
Phalcon\Config\Adapter\Yaml
Phalcon\Config\ConfigInterface
Phalcon\Di\DiInterface
Phalcon\Di\Exception
Phalcon\Di\Exception\ServiceResolutionException
Phalcon\Di\InitializationAwareInterface
Phalcon\Di\InjectionAwareInterface
Phalcon\Di\Service
Phalcon\Di\ServiceInterface
Phalcon\Di\ServiceProviderInterface
Phalcon\Events\ManagerInterface
-
Extends
-
Implements
DiInterface
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();
Properties¶
/**
* List of registered services
*
* @var ServiceInterface[]
*/
protected $services;
/**
* List of shared instances
*
* @var array
*/
protected $sharedInstances;
/**
* Events Manager
*
* @var ManagerInterface|null
*/
protected $eventsManager;
/**
* Latest DI build
*
* @var DiInterface|null
*/
protected $static $_default;
Methods¶
Magic method to get or set services using setters/getters Phalcon\Di constructorpublic function attempt( string $name, mixed $definition, bool $shared = bool ): ServiceInterface | bool;
And the services can be specified in the file as:
return [
'myComponent' => [
'className' => '\Acme\Components\MyComponent',
'shared' => true,
],
'group' => [
'className' => '\Acme\Group',
'arguments' => [
[
'type' => 'service',
'service' => 'myComponent',
],
],
],
'user' => [
'className' => '\Acme\User',
],
];
@link https://docs.phalcon.io/en/latest/reference/di.html
Loads services from a yaml file.$di->loadFromYaml(
"path/services.yaml",
[
"!approot" => function ($value) {
return dirname(__DIR__) . $value;
}
]
);
And the services can be specified in the file as:
myComponent:
className: \Acme\Components\MyComponent
shared: true
group:
className: \Acme\Group
arguments:
- type: service
name: myComponent
user:
className: \Acme\User
@link https://docs.phalcon.io/en/latest/reference/di.html
Check if a service is registered using the array syntax Allows to obtain a shared service using the array syntax Allows to register a shared service using the array syntax Removes a service from the services container using the array syntax Registers a service provider.use Phalcon\Di\DiInterface;
use Phalcon\Di\ServiceProviderInterface;
class SomeServiceProvider implements ServiceProviderInterface
{
public function register(DiInterface $di)
{
$di->setShared(
'service',
function () {
// ...
}
);
}
}
Di\AbstractInjectionAware ¶
-
Namespace
Phalcon\Di
-
Uses
-
Extends
-
Implements
InjectionAwareInterface
This abstract class offers common access to the DI in a class
Properties¶
Methods¶
Returns the internal dependency injector Sets the dependency injectorDi\DiInterface ¶
-
Namespace
Phalcon\Di
-
Uses
ArrayAccess
-
Extends
ArrayAccess
-
Implements
Interface for Phalcon\Di
Methods¶
public function attempt( string $name, mixed $definition, bool $shared = bool ): ServiceInterface | bool;
Di\Exception¶
-
Namespace
Phalcon\Di
-
Uses
-
Extends
\Exception
-
Implements
Exceptions thrown in Phalcon\Di will use this class
Di\Exception\ServiceResolutionException¶
-
Namespace
Phalcon\Di\Exception
-
Uses
-
Extends
\Phalcon\Di\Exception
-
Implements
Phalcon\Di\Exception\ServiceResolutionException
Di\FactoryDefault¶
-
Namespace
Phalcon\Di
-
Uses
Phalcon\Filter\FilterFactory
-
Extends
\Phalcon\Di\Di
-
Implements
This is a variant of the standard Phalcon\Di. By default it automatically registers all the services provided by the framework. Thanks to this, the developer does not need to register each service individually providing a full stack framework
Methods¶
Phalcon\Di\FactoryDefault constructorDi\FactoryDefault\Cli¶
-
Namespace
Phalcon\Di\FactoryDefault
-
Uses
Phalcon\Di\FactoryDefault
Phalcon\Di\Service
Phalcon\Filter\FilterFactory
-
Extends
FactoryDefault
-
Implements
Phalcon\Di\FactoryDefault\Cli
This is a variant of the standard Phalcon\Di. By default it automatically registers all the services provided by the framework. Thanks to this, the developer does not need to register each service individually. This class is specially suitable for CLI applications
Methods¶
Phalcon\Di\FactoryDefault\Cli constructorDi\Injectable ¶
-
Namespace
Phalcon\Di
-
Uses
Phalcon\Di\Di
Phalcon\Session\BagInterface
stdClass
-
Extends
stdClass
-
Implements
InjectionAwareInterface
This class allows to access services in the services container by just only accessing a public property with the same name of a registered service
Properties¶
Methods¶
Magic method __get Magic method __isset Returns the internal dependency injector Sets the dependency injectorDi\InjectionAwareInterface ¶
-
Namespace
Phalcon\Di
-
Uses
-
Extends
-
Implements
This interface must be implemented in those classes that uses internally the Phalcon\Di that creates them
Methods¶
Returns the internal dependency injector Sets the dependency injectorDi\Service¶
-
Namespace
Phalcon\Di
-
Uses
Closure
Phalcon\Di\Exception\ServiceResolutionException
Phalcon\Di\Service\Builder
-
Extends
-
Implements
ServiceInterface
Represents individually a service in the services container
$service = new \Phalcon\Di\Service(
"request",
\Phalcon\Http\Request::class
);
$request = service->resolve();
Properties¶
/**
* @var mixed
*/
protected $definition;
/**
* @var bool
*/
protected $resolved = false;
/**
* @var bool
*/
protected $shared = false;
//
protected $sharedInstance;
Methods¶
Phalcon\Di\Service Returns the service definition Returns a parameter in a specific position Returns true if the service was resolved Check whether the service is shared or not Resolves the service Set the service definition Changes a parameter in the definition without resolve the service Sets if the service is shared or not Sets/Resets the shared instance related to the serviceDi\Service\Builder¶
-
Namespace
Phalcon\Di\Service
-
Uses
Phalcon\Di\DiInterface
Phalcon\Di\Exception
-
Extends
-
Implements
Phalcon\Di\Service\Builder
This class builds instances based on complex definitions
Methods¶
Builds a service using a complex service definitionDi\ServiceInterface ¶
-
Namespace
Phalcon\Di
-
Uses
-
Extends
-
Implements
Represents a service in the services container
Methods¶
Returns the service definition Returns a parameter in a specific position Returns true if the service was resolved Check whether the service is shared or not Resolves the service Set the service definition Changes a parameter in the definition without resolve the service Sets if the service is shared or notDi\ServiceProviderInterface ¶
-
Namespace
Phalcon\Di
-
Uses
-
Extends
-
Implements
Should be implemented by service providers, or such components, which register a service in the service container.
namespace Acme;
use Phalcon\Di\DiInterface;
use Phalcon\Di\ServiceProviderInterface;
class SomeServiceProvider implements ServiceProviderInterface
{
public function register(DiInterface $di)
{
$di->setShared(
'service',
function () {
// ...
}
);
}
}