Static Class inside library folder |
[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))
[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.
[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
[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();
[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 **/
[eluser]Nicholas Hanks[/eluser]
Yes you are right. Thank you very much for your feedback. |
Welcome Guest, Not a member yet? Register Sign In |