@inject with default value (optional service)

5 years ago

matopeto
Member | 400
+
0
-

In nette 2.0 i used to write optional services as:

<?php

function injectService(Service $service = null) {
    $this->service = $service;
}

?>

That make Service optional (If service was not defined in config, it has default null value).

Is any way to do that with @inject annotation?

This doesn't work:

<?php

   /** @var Service @inject */
   public $service = null;

?>

And I know that I can use old style, but I would like use this with simple @inject annotation.

(Sorry for my bad english:))

5 years ago

enumag
Member | 2128
+
0
-

Null is the default value in PHP so this would be really wierd.

Maybe some option in the annotaion:

/** @var Service @inject(optional=TRUE) */
public $service;

5 years ago

matopeto
Member | 400
+
0
-

Thanks,

But it is only implementation suggestion? Because it is not working in Nette 2.1.*.

5 years ago

enumag
Member | 2128
+
0
-

Yeah that's only suggestion. I don't think a solution exists right now.

Last edited by enumag (2014-04-15 17:35)