[eluser]Unknown[/eluser]
I'm working on creating my first library (CI 2.1.0) and I'm having a hard time believing that every time I create a new functions that needs a CI resource I have to set it up as a varibale:
Code:
$CI =& get_instance();
I've tried creating it in a __construct() statement and that doesn't seem to work. I have a feeling I'm missing something so easy...
[eluser]CroNiX[/eluser]
It works fine in a construct. You probably just aren't accessing it correctly within the class. I'm guessing you declared it locally in your construct but didn't assign it to a property you can access from everywhere within the class.
Code:
class your_library {
public $CI;
public function __construct()
{
$this->CI =& get_instance();
}
public some_method()
{
//Access CI
$this->CI->input->post('something');
}
public some_other_method()
{
//Access CI
$this->CI->db->query();
}
}
[eluser]CroNiX[/eluser]
Because if you don't then it's only locally available to the method it was declared in (in this case the constructor) instead of globally throughout the class, just like regular procedural php.