![]() |
Tip: Sick of get_instance()? - 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: Tip: Sick of get_instance()? (/showthread.php?tid=17562) Pages:
1
2
|
Tip: Sick of get_instance()? - El Forum - 04-08-2009 [eluser]louis w[/eluser] I was growing tired of having to call get_instance to get the CI super object in all my libs and the occasional view so I set up a helper. Makes calling CI so much easier. Code: // Add to a helper and load it from your controller Just call CI() where ever you want to get Code Igniter. Code: // Example Tip: Sick of get_instance()? - El Forum - 04-08-2009 [eluser]xwero[/eluser] Where do you add the function? Tip: Sick of get_instance()? - El Forum - 04-08-2009 [eluser]louis w[/eluser] Just any helper. I have a file at helpers/misc_helpers.php that I load in my bootstrap which contains different functions i used throughout my application like pr (a better print_r), array2XML, br2nl etc. Tip: Sick of get_instance()? - El Forum - 04-08-2009 [eluser]jedd[/eluser] Because a few people are likely to cut-n-paste this, you might want to remove the extraneous ; from the get_instance line. Tip: Sick of get_instance()? - El Forum - 04-08-2009 [eluser]louis w[/eluser] [quote author="jedd" date="1239221676"]Because a few people are likely to cut-n-paste this, you might want to remove the extraneous ; from the get_instance line.[/quote] Hmm... that's pretty strange. Not in my original code and the forum keeps inserting it when I try to remove it. Not sure why it's putting that in there. Nice catch thou. Tip: Sick of get_instance()? - El Forum - 04-08-2009 [eluser]jedd[/eluser] Verily, the forums work in mysterious ways. I usually see weirdness with special characters outside of codeblocks or with indenting inside them, and the well known js script removal stuff ... but it's especially odd here. Try doing =& followed by a space - I see that construct used way more often than using the & adjacent to the function name and it doesn't seem to be afflicted thus. Tip: Sick of get_instance()? - El Forum - 04-16-2009 [eluser]meglio[/eluser] My solution is to use this class (application/libraries/Librarybase.php) Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Then I'm just using this as a base class: Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Tip: Sick of get_instance()? - El Forum - 04-16-2009 [eluser]louis w[/eluser] meglio, This is a good solution but would not work in all cases like if you wanted to access the CI super object from a helper or a view. Since these are not classes, they could not extend this base class. Tip: Sick of get_instance()? - El Forum - 04-16-2009 [eluser]meglio[/eluser] Sure, but as mentioned in the subject: Quote:tired of having to call get_instance to get the CI super object in all my libs So this will solve for libs elegantly. Tip: Sick of get_instance()? - El Forum - 04-17-2009 [eluser]Skinnpenal[/eluser] Very nice idea! Any thoughts on how this will impact performance? |