Welcome Guest, Not a member yet? Register   Sign In
Constructor for libraries
#1

[eluser]Random dude[/eluser]
Because you have to $CI =& get_instance(); in each function, and i heard you can have constructors in libraries, I thought I should try it. But my current code isn't working

Also, what syntax can I use to call functions that are within the safe library? $CI? $this?

This is the library.
Code:
class General_Functions {
    
    private $CI;
    
    function General_Functions()
    {
        $CI =& get_instance();
        $CI->load->model("get_content_model");
    }
    
    function prepare_cms_index_data( $message = "" )
    {
    
    
        $data;
        $data["pages_query"] = $CI->get_content_model->get_items_by_type("id, title", "page");
        $data["categories_and_periodicals"] = $CI->get_md_array_of_categories_and_periodicals();
        $data["message"] = $message;
        return $data;
    }

Thank you.
For now I've just gone back to initilizing $CI in each function in the library.
#2

[eluser]Cro_Crx[/eluser]
$CI is an instance variable. To access the class variable $CI up the top you need to use $this->CI

so your code should go like this if you want to reuse $CI all over

Code:
class General_Functions {
    
    private $CI;
    
    function General_Functions()
    {
        $this->CI =& get_instance();
        $this->CI->load->model("get_content_model");
    }
    
    function prepare_cms_index_data( $message = "" )
    {
        $data["pages_query"] = $this->CI->get_content_model->get_items_by_type("id, title", "page");
        $data["categories_and_periodicals"] = $this->CI->get_md_array_of_categories_and_periodicals();
        $data["message"] = $message;
        return $data;
    }

The user guide says that this won't with with PHP4 (as it's old). Here's a part of the user guide.

"Also, please note: If you are running PHP 4 it's usually best to avoid calling get_instance() from within your class constructors. PHP 4 has trouble referencing the CI super object within application constructors since objects do not exist until the class is fully instantiated."
#3

[eluser]louis w[/eluser]
I have this in a helper file which get autoloaded
Code:
function CI() {
    if (!function_exists('get_instance')) exit("Can't get CI instance");
    $ci = &get;_instance();
    return $ci;
}

You use it like this:
Code:
CI()->something->whatever();

I find that it helps quite a lot when working with libraries, or even allowing models to talk to each other.
#4

[eluser]wowdezign[/eluser]
louis w,

What about libraries loading other libraries? Does it help there? Are there any downsides?
#5

[eluser]louis w[/eluser]
You could use this to perform any CI action, so loading libraries from inside other libraries is totally possible. I have never needed to do this thou.
#6

[eluser]wowdezign[/eluser]
Maybe it isn't good practice, I don't know. But I am working on an Availability Calendar class right now that displays 3 Calendars at once.

The approach I took was to load the Calendar library into my custom library so mine could have three instances on it.

Maybe I missed a better way of doing it though.

So, I guess then, that you haven't found any downsides...

Hmmm... I'll have to try that.




Theme © iAndrew 2016 - Forum software by © MyBB