Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

DI interface injection

Hi,

I am looking into Phalcon to provide extra functionality to an existing project for a client. We will use several components for sure (annotations) and I am looking into others as well, especially DI.

In zf2 I use a pretty nift feature called "interface injection" using the initalizers config.

Basically you have the following block in the config:

'initializers' => array(
    'db' => function($service, $sm) {
        if ($service instanceof AdapterAwareInterface) {
            $service->setDbAdapter($sm->get('Zend\Db\Adapter\Adapter'));
        }
    }
)

Whenever a class is initiated using DI, the initializers will be processed. In the above example, if the class being initialized has the AdapterAwareInterface, you would inject the DB Adapter. I use this a lot to inject common things such as db adapter (to table classes for instance), logging object (to my services), service locator (to controllers and services), etc to classes.

Is something like this possible in Phalcon?

I don't want to do these common initializers in every class, if it can be done automatically based on implemented interfaces (especially custom build ones, not phalcan only ones).

Tnx Jeroen