CodeIgniter Forums

Full Version: Accessing a custom library's function in a helper
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]CMCDragonkai[/eluser]
Is this even possible? If so, what is the code required for it?

When I think of using the load, to me it only makes sense if it is used inside a class, but a helper is not a class but a group of functions.

Would I need to use 'new' instead? If so, how?

El Forum

[eluser]Glazz[/eluser]
If you want to use a function that is inside a library, you need to load the library and then use something like:

Code:
# Load the library.
$this->load->library('MyLibrary');

# Use the function...
$this->MyLibrary->MyFunction();

This answers your question?

Regards.

- G l a z z

El Forum

[eluser]CroNiX[/eluser]
If its from a helper, you need to reference the CI superglobal, just like you do when creating libraries that use CI.
Code:
function some_helper()
{
  //grab our CI instance
  $CI =& get_instance();
  //use normally using the new reference
  $CI->load->view('some_view');

  $CI->load->library('some_library');  
  $data = $CI->some_library->some_method();
  //...
}

Or, if you have already loaded the library in CI, you can reference it in your helper after grabbing the ci instance.

Code:
function some_helper($data)
{
  $CI =& get_instance();
  //library is already loaded in the controller before the helper, just access it
  $CI->some_library->some_method();
}