Part of the EllisLab Network
   
 
Tip: Sick of get_instance()?
Posted: 08 April 2009 09:23 AM   [ Ignore ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  593
Joined  02-04-2008

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.

// Add to a helper and load it from your controller
function CI() {
    
if (!function_exists('get_instance')) exit('ERROR! : ' __FILE__ .':'__FUNCTION__ ":: Can't get Code Igniter instance. Are you calling this function from within your application?");

    
$ci =& get_instance();
    return 
$ci;

Just call CI() where ever you want to get Code Igniter.

// Example
<?=CI()->buildBreadcrumb()?> 
Profile
 
 
Posted: 08 April 2009 09:43 AM   [ Ignore ]   [ # 1 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
Joined  07-14-2006

Where do you add the function?

Profile
 
 
Posted: 08 April 2009 09:55 AM   [ Ignore ]   [ # 2 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  593
Joined  02-04-2008

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.

Profile
 
 
Posted: 08 April 2009 10:14 AM   [ Ignore ]   [ # 3 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2095
Joined  06-04-2008

Because a few people are likely to cut-n-paste this, you might want to remove the extraneous ; from the get_instance line.

Profile
 
 
Posted: 08 April 2009 10:32 AM   [ Ignore ]   [ # 4 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  593
Joined  02-04-2008
jedd - 08 April 2009 10:14 AM

Because a few people are likely to cut-n-paste this, you might want to remove the extraneous ; from the get_instance line.

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.

Profile
 
 
Posted: 08 April 2009 10:54 AM   [ Ignore ]   [ # 5 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2095
Joined  06-04-2008

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.

Profile
 
 
Posted: 16 April 2009 08:56 AM   [ Ignore ]   [ # 6 ]  
Summer Student
Avatar
Total Posts:  23
Joined  08-28-2008

My solution is to use this class (application/libraries/Librarybase.php)

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Librarybase
{
    
function __construct()
    
{
        $this
->CI =& get_instance();
    
}
    
    
protected $CI;

Then I’m just using this as a base class:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Pathes extends Librarybase
{
public function anymethod()
{
  
// here you have access to $this->CI
}
Profile
 
 
Posted: 16 April 2009 09:42 AM   [ Ignore ]   [ # 7 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  593
Joined  02-04-2008

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.

Profile
 
 
Posted: 16 April 2009 09:47 AM   [ Ignore ]   [ # 8 ]  
Summer Student
Avatar
Total Posts:  23
Joined  08-28-2008

Sure, but as mentioned in the subject:

tired of having to call get_instance to get the CI super object in all my libs

So this will solve for libs elegantly.

Profile
 
 
Posted: 17 April 2009 05:53 AM   [ Ignore ]   [ # 9 ]  
Grad Student
Avatar
Rank
Total Posts:  45
Joined  09-01-2007

Very nice idea!

Any thoughts on how this will impact performance?

Profile
 
 
Posted: 17 April 2009 08:56 PM   [ Ignore ]   [ # 10 ]  
Grad Student
Avatar
Rank
Total Posts:  84
Joined  03-24-2009

Code Igniters limitation of the CI super object is one of the things that seem to give it such a speed advantage, I am sure that over-use of importing it will slow down apps somewhat. But never to the extent of Cake I am sure?

I would love to hear if anyone has any performance benchmarks of cases invoking the Super Object and not… Memory usage would also be an issue I am sure..

 Signature 

Richard McIntyre
:::PHP, Java and Ruby on Rails Development in Kansai, Japan:::
:::Follow me on twitter:::

Profile
 
 
Posted: 18 April 2009 12:33 AM   [ Ignore ]   [ # 11 ]  
Grad Student
Avatar
Rank
Total Posts:  36
Joined  08-03-2007

Is it work on PHP 4?

Profile
 
 
Posted: 19 April 2009 08:04 AM   [ Ignore ]   [ # 12 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2674
Joined  05-18-2008

I havn’t run any benchmarks on this approach, but if you plan on using this many times in a single helper function, then I would recomend just assigning the CI super object to a variable, as the overhead of making multiple calls would probably impact performance, although I’d imagine it would only make a nticable difference if you did it many, many times.

@Lima: Although the function itself would work in PHP4, the example of doing CI()->something() wouldn’t, as method chaining only works in PHP5+

 Signature 

I’m building a Project Management System for my 3rd year Uni project, Sign up to the beta
Track my progress | Post of the day: UI Designs
Get full auto complete support for CodeIgniter in Eclipse

Profile
 
 
Posted: 20 April 2009 04:11 PM   [ Ignore ]   [ # 13 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  593
Joined  02-04-2008

If you want to use this under PHP4 or worried about performance from multiple calls to it (I agree with Dam1an that this would only happen if you used it ALOT) you can do it like below. This really doesn’t do anything above the normal get_instance besides make your code look prettier and save you from having to check if it exists all the time.

$CI CI(); 
Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 148261 Total Logged-in Users: 68
Total Topics: 103041 Total Anonymous Users: 2
Total Replies: 515894 Total Guests: 477
Total Posts: 618935    
Members ( View Memberlist )