Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using phalcon's DI from singleton?

Tags:

php

phalcon

Let's imagine, that we're developing extremely simple singleton Auth class dealing with DB and session (we don't want any other decomposition like auth adapters etc).

How can I make DI available from this singleton class? Of course, best way is to pass DI instance to constructor, but what if I want it from singleton? Should I use static setter method Auth::setDi($di) or better practices are available? Or maybe we should avoid singletons because we always have DI storage?

I'm sorry for my english, I hope that my question is clear.

like image 804
Kirzilla Avatar asked Aug 13 '26 02:08

Kirzilla


1 Answers

You can access the latest DI instantiated statically:

<?php

$di = Phalcon\DI::getDefault();

http://docs.phalconphp.com/en/latest/reference/di.html#accessing-the-di-in-a-static-way

like image 124
twistedxtra Avatar answered Aug 15 '26 21:08

twistedxtra