Welcome Guest, Not a member yet? Register   Sign In
Using core library inside of custom library
#1

[eluser]kyletreubig[/eluser]
I just started using CodeIgniter about a month ago--and am thoroughly impressed! I am currently working on an end-user control panel for a website my associate and I have developed. I decided to write myself some libraries of my own for systems that I know I will reuse in the future, and may be used multiple times (and in different configurations) on a single website. Specifically, I have written a photo/video gallery library and a blog/guestbook comment library, each with front-end and back-end (management) functions, associated models, and config files.

I thought I was doing everything correctly since it worked on my personal computer and on my own web server (both running PHP5), but failed to work on my client's server (running PHP4). After fixing some errors in my code that were intrinsic to PHP4 (chaining functions, like $object->row()->data_element), I now have a problem that's been baffling me for the last two days. In my custom library, I'm trying to use the pagination library. I'm able to load the library (at least I'm not given any error), but I cannot use any of its methods. I've also tried to load the pagination library in my controller, but I still can't use other library's methods in my custom library.

It hasn't helped that the hosting company has turned off error displaying in their PHP setup, so I just get a blank white page when something goes wrong.

I hope I've explained this well enough, and any advice would be well appreciated.

Thanks!

- Kyle
#2

[eluser]Colin Williams[/eluser]
Just so we don't have to stalk you around and hijack your computer, could you post some of the code that is generating errors?

Are you using the get_instance() function?

Code:
$ci =& get_instance();
$ci->load->library('pagination');
#3

[eluser]kyletreubig[/eluser]
Sorry for not posting code earlier, I was in a slight rush out the door at the time.

I was finally (for some reason this didn't work earlier when I tried it) able to get PHP errors to display, and I get the following:
Code:
Fatal error: Call to a member function on a non-object in /public_html/system/libraries/Gallery.php on line 151

which corresponds to the following section of code in my Gallery.php library file:
Code:
118        // Load pagination library
119        if ( ! class_exists('pagination'))
120            $this->CI->load->library('pagination');
121        ...
122        // Setup all pagination options in array, $config
...        ...
151        $this->CI->pagination->initialize($config);

where $this->CI is the CI super object (from & get_instance()). I've checked the log file in the system folder, and the last line of the log (with the log threshold set to 4) states that the pagination class was instantiated.
#4

[eluser]Colin Williams[/eluser]
You could get rid of checking whether or not the class exists. CI's loader class does the lazy instantiation for you.
#5

[eluser]kyletreubig[/eluser]
I believe I have come up with a solution (though it seems ugly to me). Previously, I had put
Code:
$this->CI = & get_instance
inside my class constructor. This seems to work for all of the basic classes (loader, config, uri [though this one is autoloaded, so my theory is broken already], models, etc.). But in order to get additional library classes to work within my custom library methods, I have to use & get_instance inside the actual method itself.

So my code now looks like this:
Code:
122        // Get CI super object
123        $CI = & get_instance();
124        
125        // Load pagination library
126        $CI->load->library('pagination'); // $this->CI->load->library still works also
127
128        // Set pagination settings (in array, $config)
...        ...
157        $CI->pagination->initialize($config);

This doesn't seem very practical since now I have to get the CI super object in each method, rather doing it in the constructor. Any ideas why I need to do this for PHP4 and not PHP5?
#6

[eluser]Colin Williams[/eluser]
Works for me in PHP 4 :question:




Theme © iAndrew 2016 - Forum software by © MyBB