CodeIgniter Forums
Accessing a custom library's function in a helper - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Accessing a custom library's function in a helper (/showthread.php?tid=39007)



Accessing a custom library's function in a helper - El Forum - 02-26-2011

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


Accessing a custom library's function in a helper - El Forum - 02-26-2011

[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


Accessing a custom library's function in a helper - El Forum - 02-27-2011

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