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

little syntax details

Hello Phalcon Team,

I am new to phalcon, yet familiar with PHP webdevelopment, my question is, in this code:

$di = $di ?: new \Phalcon\Di\FactoryDefault();

If $di is Null or Empty, instantiate coming from all services that needs to be injected into the service container? And, is this similar to Dependency Injection with CLOSURES/ANONYMOUS FUNCTIONS?

Thanks very much.

CPA.IScoder

edited Mar '17

Services will be created only when accesed to them if i understand your question correctly, well at least if they are not something like:

$di->set('abc', new Abc());
edited Mar '17

I don't really understand the question either x)

But as a plus info, that line would create a new instance of the DI, so if you have a global DI created elsewhere with already attached services, you cannot access them with that particular $di variable in your code.

BUT! If you use the static method $di = $di ?: \Phalcon\Di\FactoryDefault::getDefault();, that will return the first DI instance that you've created in your code flow (or any DI you've set as default last).

I don't really understand the question either x)

But as a plus info, that line would create a new instance of the DI, so if you have a global DI created elsewhere with already attached services, you cannot access them with that particular $di variable in your code.

BUT! If you use the static method $di = $di ?: \Phalcon\Di\FactoryDefault::getDefault();, that will return the first DI instance that you've created in your code flow (or any DI you've set as default last).

Sorry Lajos, I got it... Thanks very much