Welcome Guest, Not a member yet? Register   Sign In
Am I missing something?
#1

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

[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();
  }
}
#3

[eluser]Unknown[/eluser]
the only difference in your code, which is working, is
Code:
$this->
for both the construct and the function. I'm not sure why this makes a difference but I'm glad it does. Thanks!!
#4

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





Theme © iAndrew 2016 - Forum software by © MyBB