How to call library method from library |
[eluser]SneakyDave[/eluser]
I'm using PHP 4.3.0 (don't ask), and CI 1.7.1, and I'm having the following problem. I have a method in a library that needs to call another method from within the same library to do some processing on an array. The code looks like this: Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); I get a PHP syntax error: "Non-existent class: mylibrary", pointing to the 2nd line in the "do_something" method. Whether I call the do_something_else() method as "$this->do_something_else()" or "$CI->do_something_else()" or "do_something_else()", I get the same error. I seem to recall in PHP5, that I can call the method within the library as "$this->do_something_else", but I don't have my notes with me to confirm that at the moment. What am I doing wrong?
[eluser]pistolPete[/eluser]
Could you please post the full error message? Btw: Since you do not use any CI related functions / objects you don't need to call get_instance() in every function.
[eluser]SneakyDave[/eluser]
Well, basically, it seems no matter how I call the function/method inside the library, I get this error: Fatal error: Call to undefined function: do_something_else() in blahblahblah/libraries/mylibrary.php on line xxx So I still don't know the proper way to call a library method from within the library. These types of calls don't seem to work: Code: $this->mylibrary->do_something_else
[eluser]InsiteFX[/eluser]
Sounds like you did not load the library first! Thats why your getting an error, for you to call another function in the library it needs to be loaded by CodeIgniter. Or you do not have your library name correct. Also you are missing your libraries Constructor Method! Enjoy InsiteFX
[eluser]SneakyDave[/eluser]
I have my library autoloaded. All the functions in the library work from the controller. For whatever reason, I can't call any functions in the library from within the library itself. Code: Also you are missing your libraries Constructor Method! http://ellislab.com/codeigniter/user-gui...aries.html And this note from the page... Code: 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. Are you saying that I need to load the library from within the library function itself? I'll try that, but I don't think it'll work. Again, my library functions work fine from a controller. Currently, I have the above problem resolved by calling both functions from the controller, but it's messy. It is only when I need to call a library function from within the library itself that I can't find the correct syntax. I think it's a PHP4 issue, but I need to go through a few PHP5 projects that I have working to verify that. And it doesn't seem to be a CI version issue, both 1.7.1 and 1.7.2 act the same way.
[eluser]BrianDHall[/eluser]
Try this: Code: class Mylibrary{ Then in a controller: Code: $this->load->library('mylibrary'); That should work as far as I can determine.
[eluser]InsiteFX[/eluser]
Hi, Try this using a private method. Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Enjoy InsiteFX
[eluser]SneakyDave[/eluser]
Yes, thanks! That worked exactly as it should. I didn't think the CI private methods worked in libraries, just controllers, but I guess I learned something today.
|
Welcome Guest, Not a member yet? Register Sign In |