Skip to content

Phalcon Di

Updated View as Markdown

Di\AbstractInjectionAware

AbstractSource on GitHub

This abstract class offers common access to the DI in a class

Uses stdClass

Method Summary

Properties

protectedDiInterface$containerDependency Injector

Methods

Public · 2

getDI()

public function getDI(): DiInterface;

Returns the internal dependency injector

setDI()

public function setDI( DiInterface $container ): void;

Sets the dependency injector

Di\Di

ClassSource on GitHub

Phalcon\Di\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\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\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();

Uses Phalcon\Config\Adapter\Php · Phalcon\Config\Adapter\Yaml · Phalcon\Config\ConfigInterface · Phalcon\Di\DiInterface · Phalcon\Di\Exception · Phalcon\Di\Exception\ServiceResolutionException · Phalcon\Di\Exceptions\AliasAlreadyInUse · Phalcon\Di\Exceptions\AliasNameMustBeString · Phalcon\Di\Exceptions\CircularAliasReference · Phalcon\Di\Exceptions\ServiceCannotBeResolved · Phalcon\Di\InitializationAwareInterface · Phalcon\Di\InjectionAwareInterface · Phalcon\Di\Service · Phalcon\Di\ServiceInterface · Phalcon\Di\ServiceProviderInterface · Phalcon\Events\ManagerInterface

Method Summary

publicmixed|null__call(string$method,array$arguments = [])Magic method to get or set services using setters/getterspublic__construct()Phalcon\Di\Di constructorpublicServiceInterface|boolattempt(string$name,mixed$definition,bool$shared = false)Attempts to register a service in the services containerpublicmixedget(string$name,mixed$parameters = null)Resolves the service based on its configurationpublicstringgetAlias( string$name )Return the alias based on a passed key. Returns an empty string ifpublicDiInterface|nullgetDefault()Return the latest DI createdpublicManagerInterface|nullgetInternalEventsManager()Returns the internal event managerpublicmixedgetRaw( string$name )Returns a service definition without resolvingpublicServiceInterfacegetService( string$name )Returns a Phalcon\Di\Service instancepublicServiceInterface[]getServices()Return the services registered in the DIpublicmixedgetShared(string$name,mixed$parameters = null)Resolves a service, the resolved service is stored in the DI, subsequentpublicboolhas( string$name )Check whether the DI contains a service by a namepublicboolhasShared( string$name )Check whether the DI has a cached shared instance for a service name.publicvoidloadFromPhp( string$filePath )Loads services from a php config file.publicvoidloadFromYaml(string$filePath,array|null$callbacks = null)Loads services from a yaml file.publicbooloffsetExists( mixed$name )Check if a service is registered using the array syntaxpublicmixedoffsetGet( mixed$name )Allows to obtain a shared service using the array syntaxpublicvoidoffsetSet(mixed$offset,mixed$value)Allows to register a shared service using the array syntaxpublicvoidoffsetUnset( mixed$name )Removes a service from the services container using the array syntaxpublicvoidregister( ServiceProviderInterface$provider )Registers a service provider.publicvoidremove( string$name )Removes a service in the services containerpublicvoidremoveShared( string$name )Removes the cached shared instance for a service, leaving the servicepublicvoidreset()Resets the internal default DIpublicServiceInterfaceset(string$name,mixed$definition,bool$shared = false)Registers a service in the services containerpublicselfsetAlias(string$name,mixed$aliases)Sets one or more aliases to the given name.publicvoidsetDefault( DiInterface$container )Set a default dependency injection container to be obtained into staticpublicsetInternalEventsManager( ManagerInterface$eventsManager )Sets the internal event managerpublicServiceInterfacesetService(string$name,ServiceInterface$rawDefinition)Sets a service using a raw Phalcon\Di\Service definitionpublicServiceInterfacesetShared(string$name,mixed$definition)Registers an "always shared" service in the services containerprotectedvoidloadFromConfig( ConfigInterface$config )Loads services from a Config object.

Properties

protectedarray$aliases = []List of service aliases
protectedDiInterface|null$defaultContainer = nullLatest DI build
protectedManagerInterface|null$eventsManager = nullEvents Manager
protectedServiceInterface[]$services = []List of registered services
protectedarray$sharedInstances = []List of shared instances

Methods

Public · 29

__call()

public function __call(
    string $method,
    array $arguments = []
): mixed|null;

Magic method to get or set services using setters/getters

__construct()

public function __construct();

Phalcon\Di\Di constructor

attempt()

public function attempt(
    string $name,
    mixed $definition,
    bool $shared = false
): ServiceInterface|bool;

Attempts to register a service in the services container Only is successful if a service hasn’t been registered previously with the same name

get()

public function get(
    string $name,
    mixed $parameters = null
): mixed;

Resolves the service based on its configuration

getAlias()

public function getAlias( string $name ): string;

Return the alias based on a passed key. Returns an empty string if the alias does not exist

getDefault()

public static function getDefault(): DiInterface|null;

Return the latest DI created

getInternalEventsManager()

public function getInternalEventsManager(): ManagerInterface|null;

Returns the internal event manager

getRaw()

public function getRaw( string $name ): mixed;

Returns a service definition without resolving

getService()

public function getService( string $name ): ServiceInterface;

Returns a Phalcon\Di\Service instance

getServices()

public function getServices(): ServiceInterface[];

Return the services registered in the DI

getShared()

public function getShared(
    string $name,
    mixed $parameters = null
): mixed;

Resolves a service, the resolved service is stored in the DI, subsequent requests for this service will return the same instance

has()

public function has( string $name ): bool;

Check whether the DI contains a service by a name

hasShared()

public function hasShared( string $name ): bool;

Check whether the DI has a cached shared instance for a service name.

Unlike has(), which reports on the service definition registry, this method reports only on the resolved-instance cache populated by getShared().

loadFromPhp()

public function loadFromPhp( string $filePath ): void;

Loads services from a php config file.

$di->loadFromPhp("path/services.php");

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/latest/di/

loadFromYaml()

public function loadFromYaml(
    string $filePath,
    array|null $callbacks = null
): void;

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/latest/di/

offsetExists()

public function offsetExists( mixed $name ): bool;

Check if a service is registered using the array syntax

offsetGet()

public function offsetGet( mixed $name ): mixed;

Allows to obtain a shared service using the array syntax

var_dump($di["request"]);

offsetSet()

public function offsetSet(
    mixed $offset,
    mixed $value
): void;

Allows to register a shared service using the array syntax

$di["request"] = new \Phalcon\Http\Request();

offsetUnset()

public function offsetUnset( mixed $name ): void;

Removes a service from the services container using the array syntax

register()

public function register( ServiceProviderInterface $provider ): void;

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 () {
                // ...
            }
        );
    }
}

remove()

public function remove( string $name ): void;

Removes a service in the services container It also removes any shared instance created for the service

removeShared()

public function removeShared( string $name ): void;

Removes the cached shared instance for a service, leaving the service definition intact so the next getShared() call rebuilds it.

reset()

public static function reset(): void;

Resets the internal default DI

set()

public function set(
    string $name,
    mixed $definition,
    bool $shared = false
): ServiceInterface;

Registers a service in the services container

setAlias()

public function setAlias(
    string $name,
    mixed $aliases
): self;

Sets one or more aliases to the given name.

setDefault()

public static function setDefault( DiInterface $container ): void;

Set a default dependency injection container to be obtained into static methods

setInternalEventsManager()

public function setInternalEventsManager( ManagerInterface $eventsManager );

Sets the internal event manager

setService()

public function setService(
    string $name,
    ServiceInterface $rawDefinition
): ServiceInterface;

Sets a service using a raw Phalcon\Di\Service definition

setShared()

public function setShared(
    string $name,
    mixed $definition
): ServiceInterface;

Registers an “always shared” service in the services container

Protected · 1

loadFromConfig()

protected function loadFromConfig( ConfigInterface $config ): void;

Loads services from a Config object.

Di\DiInterface

InterfaceSource on GitHub

Interface for Phalcon\Di\Di

  • \ArrayAccess
    • Phalcon\Di\DiInterface

Uses ArrayAccess

Method Summary

publicServiceInterface|boolattempt(string$name,mixed$definition,bool$shared = false)Attempts to register a service in the services containerpublicmixedget(string$name,mixed$parameters = null)Resolves the service based on its configurationpublicDiInterface|nullgetDefault()Return the last DI createdpublicmixedgetRaw( string$name )Returns a service definition without resolvingpublicServiceInterfacegetService( string$name )Returns the corresponding Phalcon\Di\Service instance for a servicepublicServiceInterface[]getServices()Return the services registered in the DIpublicmixedgetShared(string$name,mixed$parameters = null)Returns a shared service based on their configurationpublicboolhas( string$name )Check whether the DI contains a service by a namepublicboolhasShared( string$name )Check whether the DI has a cached shared instance for a service name.publicvoidremove( string$name )Removes a service in the services containerpublicvoidremoveShared( string$name )Removes the cached shared instance for a service, leaving the servicepublicvoidreset()Resets the internal default DIpublicServiceInterfaceset(string$name,mixed$definition,bool$shared = false)Registers a service in the services containerpublicvoidsetDefault( DiInterface$container )Set a default dependency injection container to be obtained into staticpublicServiceInterfacesetService(string$name,ServiceInterface$rawDefinition)Sets a service using a raw Phalcon\Di\Service definitionpublicServiceInterfacesetShared(string$name,mixed$definition)Registers an "always shared" service in the services container

Methods

Public · 16

attempt()

public function attempt(
    string $name,
    mixed $definition,
    bool $shared = false
): ServiceInterface|bool;

Attempts to register a service in the services container Only is successful if a service hasn’t been registered previously with the same name

get()

public function get(
    string $name,
    mixed $parameters = null
): mixed;

Resolves the service based on its configuration

getDefault()

public static function getDefault(): DiInterface|null;

Return the last DI created

getRaw()

public function getRaw( string $name ): mixed;

Returns a service definition without resolving

getService()

public function getService( string $name ): ServiceInterface;

Returns the corresponding Phalcon\Di\Service instance for a service

getServices()

public function getServices(): ServiceInterface[];

Return the services registered in the DI

getShared()

public function getShared(
    string $name,
    mixed $parameters = null
): mixed;

Returns a shared service based on their configuration

has()

public function has( string $name ): bool;

Check whether the DI contains a service by a name

hasShared()

public function hasShared( string $name ): bool;

Check whether the DI has a cached shared instance for a service name.

Unlike has(), which reports on the service definition registry, this method reports only on the resolved-instance cache populated by getShared(). A service can be registered (has() returns true) without yet having a shared instance (hasShared() returns false).

remove()

public function remove( string $name ): void;

Removes a service in the services container

removeShared()

public function removeShared( string $name ): void;

Removes the cached shared instance for a service, leaving the service definition intact so the next getShared() call rebuilds it.

Useful in fork-based multi-process setups where a child inherits the parent’s resource handle (e.g. a database connection) and needs to discard the cached instance without re-registering the service.

reset()

public static function reset(): void;

Resets the internal default DI

set()

public function set(
    string $name,
    mixed $definition,
    bool $shared = false
): ServiceInterface;

Registers a service in the services container

setDefault()

public static function setDefault( DiInterface $container ): void;

Set a default dependency injection container to be obtained into static methods

setService()

public function setService(
    string $name,
    ServiceInterface $rawDefinition
): ServiceInterface;

Sets a service using a raw Phalcon\Di\Service definition

setShared()

public function setShared(
    string $name,
    mixed $definition
): ServiceInterface;

Registers an “always shared” service in the services container

Di\Exception

ClassSource on GitHub

Exceptions thrown in Phalcon\Di will use this class

Method Summary

Methods

Public · 4

serviceCannotBeResolved()

public static function serviceCannotBeResolved( string $name ): Exception;

serviceNotFound()

public static function serviceNotFound( string $name ): Exception;

undefinedMethod()

public static function undefinedMethod( string $method ): Exception;

unknownServiceInParameter()

public static function unknownServiceInParameter( int $position ): Exception;

Di\Exception\ServiceResolutionException

ClassSource on GitHub

Phalcon\Di\Exception\ServiceResolutionException

Di\Exceptions\AliasAlreadyInUse

ClassSource on GitHub

Uses Phalcon\Di\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct( string $alias );

Di\Exceptions\AliasNameMustBeString

ClassSource on GitHub

Uses Phalcon\Di\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Di\Exceptions\ArgumentTypeRequired

ClassSource on GitHub

Uses Phalcon\Di\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct( int $position );

Di\Exceptions\CallArgumentsMustBeArray

ClassSource on GitHub

Uses Phalcon\Di\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct( int $position );

Di\Exceptions\CircularAliasReference

ClassSource on GitHub

Uses Phalcon\Di\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct( string $name );

Di\Exceptions\ContainerRequired

ClassSource on GitHub

Uses Phalcon\Di\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Di\Exceptions\DefinitionMustBeArrayForRead

ClassSource on GitHub

Uses Phalcon\Di\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Di\Exceptions\DefinitionMustBeArrayForUpdate

ClassSource on GitHub

Uses Phalcon\Di\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Di\Exceptions\MethodCallMustBeArray

ClassSource on GitHub

Uses Phalcon\Di\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct( int $position );

Di\Exceptions\MethodNameRequired

ClassSource on GitHub

Uses Phalcon\Di\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct( int $position );

Di\Exceptions\MissingClassNameParameter

ClassSource on GitHub

Uses Phalcon\Di\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Di\Exceptions\MissingParameterKey

ClassSource on GitHub

Uses Phalcon\Di\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct(
    string $key,
    int $position
);

Di\Exceptions\PropertyInjectionRequiresInstance

ClassSource on GitHub

Uses Phalcon\Di\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Di\Exceptions\PropertyMustBeArray

ClassSource on GitHub

Uses Phalcon\Di\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct( int $position );

Di\Exceptions\PropertyNameRequired

ClassSource on GitHub

Uses Phalcon\Di\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct( int $position );

Di\Exceptions\PropertyValueRequired

ClassSource on GitHub

Uses Phalcon\Di\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct( int $position );

Di\Exceptions\ServiceCannotBeResolved

ClassSource on GitHub

Uses Phalcon\Di\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct( string $name );

Di\Exceptions\SetterInjectionRequiresInstance

ClassSource on GitHub

Uses Phalcon\Di\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Di\Exceptions\SetterParametersMustBeArray

ClassSource on GitHub

Uses Phalcon\Di\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Di\Exceptions\UnknownServiceType

ClassSource on GitHub

Uses Phalcon\Di\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct( int $position );

Di\FactoryDefault

ClassSource on GitHub

This is a variant of the standard Phalcon\Di\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

Uses Phalcon\Annotations\Adapter\Memory · Phalcon\Assets\Manager · Phalcon\Encryption\Crypt · Phalcon\Encryption\Security · Phalcon\Events\Manager · Phalcon\Filter\FilterFactory · Phalcon\Flash\Direct · Phalcon\Flash\Session · Phalcon\Html\Escaper · Phalcon\Html\TagFactory · Phalcon\Http\Request · Phalcon\Http\Response · Phalcon\Http\Response\Cookies · Phalcon\Mvc\Dispatcher · Phalcon\Mvc\Model\Manager · Phalcon\Mvc\Model\MetaData\Memory · Phalcon\Mvc\Model\Transaction\Manager · Phalcon\Mvc\Router · Phalcon\Mvc\Url · Phalcon\Queue\QueueFactory · Phalcon\Support\HelperFactory · Phalcon\Support\Settings

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Phalcon\Di\FactoryDefault constructor

Di\FactoryDefault\Cli

ClassSource on GitHub

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

Uses Phalcon\Annotations\Adapter\Memory · Phalcon\Cli\Dispatcher · Phalcon\Cli\Router · Phalcon\Di\FactoryDefault · Phalcon\Di\Service · Phalcon\Encryption\Security · Phalcon\Events\Manager · Phalcon\Filter\FilterFactory · Phalcon\Html\Escaper · Phalcon\Html\TagFactory · Phalcon\Mvc\Model\Manager · Phalcon\Mvc\Model\MetaData\Memory · Phalcon\Mvc\Model\Transaction\Manager · Phalcon\Queue\QueueFactory · Phalcon\Support\HelperFactory · Phalcon\Support\Settings

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Phalcon\Di\FactoryDefault\Cli constructor

Di\InitializationAwareInterface

InterfaceSource on GitHub

Interface for components that have initialize()

  • Phalcon\Di\InitializationAwareInterface

Method Summary

Methods

Public · 1

initialize()

public function initialize(): void;

Di\Injectable

AbstractSource on GitHub

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

@property \Phalcon\Mvc\Dispatcher|\Phalcon\Mvc\DispatcherInterface $dispatcher @property \Phalcon\Mvc\Router|\Phalcon\Mvc\RouterInterface $router @property \Phalcon\Mvc\Url|\Phalcon\Mvc\Url\UrlInterface $url @property \Phalcon\Http\Request|\Phalcon\Http\RequestInterface $request @property \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface $response @property \Phalcon\Http\Response\Cookies|\Phalcon\Http\Response\CookiesInterface $cookies @property \Phalcon\Filter\Filter $filter @property \Phalcon\Flash\Direct $flash @property \Phalcon\Flash\Session $flashSession @property \Phalcon\Session\ManagerInterface $session @property \Phalcon\Events\Manager|\Phalcon\Events\ManagerInterface $eventsManager @property \Phalcon\Db\Adapter\AdapterInterface $db @property \Phalcon\Encryption\Security $security @property \Phalcon\Encryption\Crypt|\Phalcon\Encryption\Crypt\CryptInterface $crypt @property \Phalcon\Html\TagFactory $tag @property \Phalcon\Html\Escaper|\Phalcon\Html\Escaper\EscaperInterface $escaper @property \Phalcon\Annotations\Adapter\Memory|\Phalcon\Annotations\Adapter $annotations @property \Phalcon\Mvc\Model\Manager|\Phalcon\Mvc\Model\ManagerInterface $modelsManager @property \Phalcon\Mvc\Model\MetaData\Memory|\Phalcon\Mvc\Model\MetadataInterface $modelsMetadata @property \Phalcon\Mvc\Model\Transaction\Manager|\Phalcon\Mvc\Model\Transaction\ManagerInterface $transactionManager @property \Phalcon\Support\Settings $settings @property \Phalcon\Assets\Manager $assets @property \Phalcon\Di\Di|\Phalcon\Di\DiInterface $di @property \Phalcon\Session\Bag|\Phalcon\Session\BagInterface $persistent @property \Phalcon\Mvc\View|\Phalcon\Mvc\ViewInterface $view

Uses Phalcon\Di\Di · Phalcon\Di\Exceptions\ContainerRequired · Phalcon\Session\BagInterface · stdClass

Method Summary

Properties

protectedDiInterface|null$container = nullDependency Injector

Methods

Public · 4

__get()

public function __get( string $propertyName ): mixed|null;

Magic method __get

__isset()

public function __isset( string $name ): bool;

Magic method __isset

getDI()

public function getDI(): DiInterface;

Returns the internal dependency injector

setDI()

public function setDI( DiInterface $container ): void;

Sets the dependency injector

Di\InjectionAwareInterface

InterfaceSource on GitHub

This interface must be implemented in those classes that uses internally the Phalcon\Di\Di that creates them

  • Phalcon\Di\InjectionAwareInterface

Method Summary

Methods

Public · 2

getDI()

public function getDI(): DiInterface;

Returns the internal dependency injector

setDI()

public function setDI( DiInterface $container ): void;

Sets the dependency injector

Di\Service

ClassSource on GitHub

Represents individually a service in the services container

$service = new \Phalcon\Di\Service(
    "request",
    \Phalcon\Http\Request::class
);

$request = service->resolve();

Uses Closure · Phalcon\Di\Exception\ServiceResolutionException · Phalcon\Di\Exceptions\DefinitionMustBeArrayForRead · Phalcon\Di\Exceptions\DefinitionMustBeArrayForUpdate · Phalcon\Di\Service\Builder

Method Summary

Properties

protectedmixed$definition
protectedbool$resolved = false
protectedbool$shared = false
protectedmixed|null$sharedInstance = null

Methods

Public · 10

__construct()

final public function __construct(
    mixed $definition,
    bool $shared = false
);

Phalcon\Di\Service

getDefinition()

public function getDefinition(): mixed;

Returns the service definition

getParameter()

public function getParameter( int $position );

Returns a parameter in a specific position

isResolved()

public function isResolved(): bool;

Returns true if the service was resolved

isShared()

public function isShared(): bool;

Check whether the service is shared or not

resolve()

public function resolve(
    mixed $parameters = null,
    DiInterface|null $container = null
): mixed;

Resolves the service

setDefinition()

public function setDefinition( mixed $definition ): void;

Set the service definition

setParameter()

public function setParameter(
    int $position,
    array $parameter
): ServiceInterface;

Changes a parameter in the definition without resolve the service

setShared()

public function setShared( bool $shared ): void;

Sets if the service is shared or not

setSharedInstance()

public function setSharedInstance( mixed $sharedInstance ): void;

Sets/Resets the shared instance related to the service

Di\ServiceInterface

InterfaceSource on GitHub

Represents a service in the services container

  • Phalcon\Di\ServiceInterface

Method Summary

Methods

Public · 8

getDefinition()

public function getDefinition(): mixed;

Returns the service definition

getParameter()

public function getParameter( int $position );

Returns a parameter in a specific position

isResolved()

public function isResolved(): bool;

Returns true if the service was resolved

isShared()

public function isShared(): bool;

Check whether the service is shared or not

resolve()

public function resolve(
    mixed $parameters = null,
    DiInterface|null $container = null
): mixed;

Resolves the service

setDefinition()

public function setDefinition( mixed $definition );

Set the service definition

setParameter()

public function setParameter(
    int $position,
    array $parameter
): ServiceInterface;

Changes a parameter in the definition without resolve the service

setShared()

public function setShared( bool $shared );

Sets if the service is shared or not

Di\ServiceProviderInterface

InterfaceSource on GitHub

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 () {
                // ...
            }
        );
    }
}
  • Phalcon\Di\ServiceProviderInterface

Method Summary

Methods

Public · 1

register()

public function register( DiInterface $di ): void;

Registers a service provider.

Di\Service\Builder

ClassSource on GitHub

Phalcon\Di\Service\Builder

This class builds instances based on complex definitions

  • Phalcon\Di\Service\Builder

Uses Phalcon\Di\DiInterface · Phalcon\Di\Exception · Phalcon\Di\Exceptions\ArgumentTypeRequired · Phalcon\Di\Exceptions\CallArgumentsMustBeArray · Phalcon\Di\Exceptions\MethodCallMustBeArray · Phalcon\Di\Exceptions\MethodNameRequired · Phalcon\Di\Exceptions\MissingClassNameParameter · Phalcon\Di\Exceptions\MissingParameterKey · Phalcon\Di\Exceptions\PropertyInjectionRequiresInstance · Phalcon\Di\Exceptions\PropertyMustBeArray · Phalcon\Di\Exceptions\PropertyNameRequired · Phalcon\Di\Exceptions\PropertyValueRequired · Phalcon\Di\Exceptions\SetterInjectionRequiresInstance · Phalcon\Di\Exceptions\SetterParametersMustBeArray · Phalcon\Di\Exceptions\UnknownServiceType

Method Summary

Methods

Public · 1

build()

public function build(
    DiInterface $container,
    array $definition,
    mixed $parameters = null
);

Builds a service using a complex service definition

Type to search…

↑↓ navigate↵ selectEsc close