![]() |
Creating and using a library - 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: Creating and using a library (/showthread.php?tid=60770) Pages:
1
2
|
Creating and using a library - El Forum - 06-24-2014 [eluser]dwlamb[/eluser] For the first time, I am constructing a library for some functions that need to be accessed by more than one controller. After reading 'Passing Parameters When Initializing Your Class' in the documentation I am unsure if I need to set-up initializing to accept parameters or if the function will accept a variable I pass to it when it is called from a controller. For instance, I have these lines in a controller: Code: function edit_content { With moving the function urls to a library, would the syntax in the controller become?: Code: function edit_content { From reading the documentation, this is what I have for defining my library: Code: file named Urls_library.php Creating and using a library - El Forum - 06-24-2014 [eluser]treenef[/eluser] Looks OK to me, have you tried testing it? Creating and using a library - El Forum - 06-24-2014 [eluser]dwlamb[/eluser] It did not work. :roll: I am loading the library through the controller with: Code: $this->load->library('urls_library'); I have tried with the index function and at the beginning of the function that makes a call for a function in the library. This is the error message: Code: A PHP Error was encountered Creating and using a library - El Forum - 06-24-2014 [eluser]treenef[/eluser] Post your user library code, and controller code specifically line 174. Creating and using a library - El Forum - 06-24-2014 [eluser]dwlamb[/eluser] I have tried to call the library using function index() of the controller user.php Code: function index() { ...and at the start of the function which makes a call to the library on line 174 Code: function edit_content() { This is the content of Urls_library.php. It is saved to application/libraries/. I checked permissions. They are 666. Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); I am really stumped. Creating and using a library - El Forum - 06-24-2014 [eluser]rm_beginner[/eluser] i am very new in code_igniter try to add this for you to get all CI core instances. $CI =& get_instance(); $CI->load->library('urls_library'); http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html Creating and using a library - El Forum - 06-25-2014 [eluser]treenef[/eluser] $data['url_pit'] = $this->Urls_library->make_urls($urls_data); // Make sure the call to the library is all lower case on this line: $data['url_pit'] = $this->urls_library->make_urls($urls_data); // Creating and using a library - El Forum - 06-25-2014 [eluser]dwlamb[/eluser] I do not understand why this would make a difference but modifying all of the names to remove underscores (_) fixed the problem. Not simply function names but the class name and the library file. No more error messages emanating from line 174. New rule: No underscores in function names or library class name when creating a library. Creating and using a library - El Forum - 06-25-2014 [eluser]CroNiX[/eluser] Strange, my custom libraries use underscores in the filename, class name and function names with no problems. I think treenef had the right solution in his last post. You were capitalizing when it should be all lowercase. Did you try it the way he suggested? Creating and using a library - El Forum - 06-25-2014 [eluser]treenef[/eluser] Yes... removing the underscores was just a coincidence as you probably removed the capitals as well at the same time, thereby prompting you to conclude that the underscores was a problem. It is not this. It was the fact that you had a capital 'U' when you called the library from your controller. Read the documentation for further clarification. I know there are some issues with names being capitalized depending on what OS you are using either linux or windows. If in doubt refer to the documentation. |