CodeIgniter Forums

Full Version: Easier access to objects ($this->object instead of $this->CI->object)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
If you want to access objects in CI libraries you need to do something like this:
Code:
class Some_class {

    protected $CI;

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

    function some_function()
    {
        // Access to loader class
        $this->CI->load->library('form_validation');

        // Access to form_validation class
        $this->CI->form_validation->run();
    }

}

But you can use $this->object instead of $this->CI->object.

Everything you need is to create a new library with following code and save it as ./application/libraries/Library.php.
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Library {
    
    protected $CI;
    
    function initialize()
    {
        $this->CI =& get_instance();
    }
    
    function __get($key)
    {
        return $this->CI->$key;
    }
    
}


// END Library class

/* End of file Library.php */
/* Location: ./application/libraries/Library.php */

Then you need to autoload this new class.
Code:
$autoload['libraries'] = array('library', 'database', 'form_validation' .....);

Any class in which you want to use $this->object instead of $this->CI->object must extends library 'Library'. You need not to declare any constructor method anymore. So, the new code of the original class will be ...
Code:
class Some_class extends Library {

    function some_function()
    {
        // Access to loader class without $this->CI->
        $this->load->library('form_validation');

        // Access to form_validation class without $this->CI->
        $this->form_validation->run();
    }

}

I know... my english is really bad, but I hope, I helped you with writing more effective PHP code.

Thanks for reading ...

El Forum

[eluser]NeoArc[/eluser]
I'd love this:
Code:
$validation = new CI_Form_Validation();
$this->view_campaings = CI_Model::new('campaings_model'); //Or just: new Campaings_model();

El Forum

[eluser]Aken[/eluser]
The initialize() function needs to be __construct() - there's nothing that will automatically call initialize(). Other than that, it's a good trick - I've used similar styles, but I don't like autoloading default libraries like that, so I just did an __autoload() instead.

NeoArc, you could create an __autoload() with appropriate code to check for all locations for the requested class, but overall CodeIgniter isn't built to work that way. Maybe you'd prefer a different framework.

El Forum

[eluser]Vheissu[/eluser]
Cool idea man. I usually use a similar approach when using CI Driver functionality. I have the parent driver class using a __call which will map both calls to a function and any arguments supplied. This is a nice touch for accessing the CI super object.