Welcome Guest, Not a member yet? Register   Sign In
Tip: Sick of get_instance()?
#1

[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
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.

Code:
// Example
<?=CI()->buildBreadcrumb()?>
#2

[eluser]xwero[/eluser]
Where do you add the function?
#3

[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.
#4

[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.
#5

[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.
#6

[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.
#7

[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');

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

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

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

class Pathes extends Librarybase
{
public function anymethod()
{
  // here you have access to $this->CI
}
}
#8

[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.
#9

[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.
#10

[eluser]Skinnpenal[/eluser]
Very nice idea!

Any thoughts on how this will impact performance?




Theme © iAndrew 2016 - Forum software by © MyBB