Welcome Guest, Not a member yet? Register   Sign In
PHP5 & get_instance()
#1

[eluser]wiredesignz[/eluser]
It's common practice to use the $CI super object in libraries to gain access to the current controller and use it's properties/methods, usually this is achieved by using the following code in the library constructor.
Code:
class SomeLibrary
{
    var $CI;

    function SomeLibrary()
    {
        $this->CI =& get_instance();
      
        //print_r($this);
    }
}

Some of us also notice that var_dump($this) and print_r($this) will dump the entire super object as well as our library, which can make it difficult to see where our library starts and ends within the output.

Using PHP5 allows us to set `private static` class variables, which, as it happens, are not processed by var_dump() or print_r().
Code:
class SomeLibrary
{
    private static $CI;

    function __contstruct()
    {
        self::$CI =& get_instance();
      
        //print_r($this);
    }
}

I would recommend using the last method for those of us developing with PHP5 exclusively.
#2

[eluser]m4rw3r[/eluser]
I usually don't fetch the whole CI object, just the object/lib I need. Because of that, it is not so dependent on CI or the configuration/naming of the libs/objects.

There is also a nice customizable variant of print_r() which I found on php.net in the comments to print_r().
Here it is: link

It is PHP 4 compatible and can ignore certain keys/propertynames.




Theme © iAndrew 2016 - Forum software by © MyBB