We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to specify a parent class during di service registration in phalcon?

I have been learning phalcon and trying to use a base service with certain member variables and functions. I would like to extend it in multiple other services so that i don't have to redefine the arguments/calls during service registration of child classes. How can I achieve it? eg:

class BaseManager {
    protected $logger;
    protected $extService;

    public function setLogger($logger){
        $this->logger = $logger;
    }

    public function setExtService($extService) {
        $this->extService = $extService;
    }
}

For this I have defined service registration as

$di->set('baseManager', array(
        'className' => 'Services\BaseManager',
        'calls'     => array(
            array(
                'method'    => 'setLogger',
                'arguments' => array(array('type' => 'service','name' => 'logger'))
            ),
            array(
                'method'    => 'setExtService',
                'arguments' => array(array('type' => 'service','name' => 'extService'))
            )
        )
    )
);

Now I need to define another service extending the BaseManager

class CustomManager extends BaseManager{
....
...

}

How do I need to define the service registration of the new class CustomManager? I have tried simply using

$di->set('CustomManager', array(
        'className' => 'Services\CustomManager'
    )
);

But this does not instantiate the member variables of the base. I was using Symfony2 before and it had the option of specifying "parent". What can be done in phalcon to achieve similar functionality?

Why you just can't set logget and extService in constructor of BaseManager ?

Why you just can't set logget and extService in constructor of BaseManager ? setting the logger and extService as costructor argument in BaseManager does not make them available to my customservice. what I am looking is for a way to inherit a service class in another service

What you mean does not make them available to your custom service ? It will make them available.

Instead of those calls:

'calls'     => array(
            array(
                'method'    => 'setLogger',
                'arguments' => array(array('type' => 'service','name' => 'logger'))
            ),
            array(
                'method'    => 'setExtService',
                'arguments' => array(array('type' => 'service','name' => 'extService'))
            )
        )

You can just create BaseManager which will extends Injectable or implement InjectionAwareInterface, and there you can set logger and extService which will be get from di. Then your customManager if it extends BaseManager can have acess to logger and extService too.

@Wojciech Ślawski thanks for the explaination. I can now have access to the logger and extService. But one thing which bothers me is that extinding the Injectable class in 'BaseManager' will amout to injecting the entire di in every of my managers extending 'BaseManager'. Is it a good practice to inject the entire di? Is there any other way where I can inject only a few service/parameter in the 'BaseManager' and inherit them in the extending classes?

What you mean does not make them available to your custom service ? It will make them available.

Instead of those calls:

'calls'     => array(
           array(
               'method'    => 'setLogger',
               'arguments' => array(array('type' => 'service','name' => 'logger'))
           ),
           array(
               'method'    => 'setExtService',
               'arguments' => array(array('type' => 'service','name' => 'extService'))
           )
       )

You can just create BaseManager which will extends Injectable or implement InjectionAwareInterface, and there you can set logger and extService which will be get from di. Then your customManager if it extends BaseManager can have acess to logger and extService too.

Well, you can access the Di in static way using Di::getDefault() but i wouldn't recommend it. I think in terms of performance until you are going to make like 50 managers then this should be like micro seconds.