CodeIgniter Forums
Service singletons - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: Service singletons (/showthread.php?tid=74323)



Service singletons - tony.a - 09-10-2019

Is there a reason why router ,routescollection,incomingrequest,response classes shoudn't be accesed just as singletons  inside Service class ? 
Also instantiations done inside private constructor ?


RE: Service singletons - kilishan - 09-10-2019

You definitely have that option. An instance is already provided for your convenience in the controller, but they're are a singleton grabbed from the services lib. So either way works.


RE: Service singletons - tony.a - 09-11-2019

Whats your opinion about providing only singletons in services lib and leave new class instance to developer .


RE: Service singletons - kilishan - 09-11-2019

I'm not 100% sure what you're asking for. If you're asking about taking away the possibility of getting a new instance from a core service, then I'm not a fan. In your own services you can do whatever you like, but part of the benefit of a service is having it take care of any dependency injection, etc.