Static Class inside library folder - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Static Class inside library folder (/showthread.php?tid=30759) |
Static Class inside library folder - El Forum - 05-25-2010 [eluser]Nicholas Hanks[/eluser] I tried to create static class with all static methods inside library folder however I was not able to do that because by default Codeigniter tries to instantiate the class inside the library folder which my class design does not allow. My code is like this. Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); Code: if(Data::article_exist($id)) Static Class inside library folder - El Forum - 05-25-2010 [eluser]Jelmer[/eluser] CI has no native way for loading static classes (or interfaces/abstracts). There's a couple of ways to go about it: - Load as a helper, which is the only way to load without instantiating (as you mentioned) - Load with another class by putting it in the same file (quite ugly) - Use inlcude() - Use an autoloader I went with the last option, which I described here. Static Class inside library folder - El Forum - 05-25-2010 [eluser]danmontgomery[/eluser] I'm not aware of a 'proper' singleton implementation, I use __autoload so that I don't have to use CI's loader. edit: too slow Static Class inside library folder - El Forum - 05-25-2010 [eluser]Nicholas Hanks[/eluser] Thank you very much for the responses. I have found that if you use Modular Seperation you can circumvent this problem. Only thing is that you have to change this Code: $autoload['libraries'] = array('data'); Code: $autoload['libraries'] = array(); Static Class inside library folder - El Forum - 05-25-2010 [eluser]Jelmer[/eluser] So you unknowingly went with our suggestion to use an autoloader (modular seperation comes with an autoloader for the libraries directory) It's set using the spl_autoload_register function at line 4 of MY_Router.php: Code: spl_autoload_register('Modules::autoload'); And you can find the autoloader function at lines 139-148 Code: /** Library base class autoload **/ Static Class inside library folder - El Forum - 05-26-2010 [eluser]Nicholas Hanks[/eluser] Yes you are right. Thank you very much for your feedback. |