Kira
Kira

Reputation: 569

How do you load a helper within a library in codeigniter?

I created a library for API access, and I created a seperate helper for common functions used by the library. In codeigniter, new libraries can access native classes by creating an instance of themselves using...

$example_instance = & get_instance();

I did this, loaded my helper- but every time the helper function is called i get the "trying to access a non-object" error. What am I doing wrong?

Here's what I have

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class api_example {
private $api;
public function __construct(){
    $this->api = & get_instance();
    $this->api->load->helper('helper_test');
}   

public function search_recent($param){

    $string = $this->api->helper_test->connect($url); //error!!!

    return $xml;
}


}

/* End of file  */

Upvotes: 2

Views: 9452

Answers (2)

gen_Eric
gen_Eric

Reputation: 227220

That's not how you call a function from a helper. Helper functions aren't part of the CodeIgniter object. They're just functions.

$string = connect($url);

Upvotes: 1

Ayman Safadi
Ayman Safadi

Reputation: 11552

CodeIgniter helpers should be functions, not classes.

Try simply:

$string = connect($url);

Upvotes: 5

Related Questions