![]() |
How can i load a library file in a helper file? - 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: How can i load a library file in a helper file? (/showthread.php?tid=13010) |
How can i load a library file in a helper file? - El Forum - 11-07-2008 [eluser]haraldo[/eluser] Hi there, Newbi question. How can i load a library file in a helper file? I've tryed loading the library but i get the error: PHP Fatal error: Using $this when not in object context in... I'm aware of what the error means. I could probably hack it but how in codeigniter can i do it nicely?! Thanks How can i load a library file in a helper file? - El Forum - 11-07-2008 [eluser]dcunited08[/eluser] Code: $ci = get_instance(); How can i load a library file in a helper file? - El Forum - 11-07-2008 [eluser]haraldo[/eluser] sweet! Thank you How can i load a library file in a helper file? - El Forum - 11-07-2008 [eluser]MMacdonald[/eluser] What are you trying to achieve here? Helpers are meant to be small, procedural functions for carrying out simple tasks. The syntax below works, but I'm not convinced that calling libraries within helpers is "good practice". I'd be interested to know other peoples' opinions. Controller Code: function index() Helper Code: function example_helper($controller_name) How can i load a library file in a helper file? - El Forum - 11-07-2008 [eluser]dcunited08[/eluser] [quote author="MMacdonald" date="1226085009"]What are you trying to achieve here? Helpers are meant to be small, procedural functions for carrying out simple tasks. The syntax below works, but I'm not convinced that calling libraries within helpers is "good practice". I'd be interested to know other peoples' opinions.[/quote] I would tend to agree with you and almost put that in my earlier response. It is possible to do it that way but I would not make a habit of it. My question would be why are you using a helper and not a library for this? That being said, I have done it once when I could not see another, easier option. My function loaded all the views in a particular folder. How can i load a library file in a helper file? - El Forum - 11-07-2008 [eluser]haraldo[/eluser] My code is as below: Code: function resize_image( $file_path ) { How can i load a library file in a helper file? - El Forum - 11-07-2008 [eluser]dcunited08[/eluser] I would just call the library directly and add a config file for it to hold the default values, that seems why you are putting it in a function. Another option, subclass it as MY_image_lib and create a static method that does what this does. Code: class MY_Image_lib extends CI_Image_lib{ That way you can call it: Code: $return = MY_Image_lib::resize_image($file); How can i load a library file in a helper file? - El Forum - 11-07-2008 [eluser]haraldo[/eluser] Thanks for the input. I may just do somrthing like that. If you have time, would you explain your theory as to why you would do it this way, over the way i'm doing it. Many thanks, How can i load a library file in a helper file? - El Forum - 11-07-2008 [eluser]dcunited08[/eluser] [quote author="haraldo" date="1226087676"]Thanks for the input. I may just do somrthing like that. If you have time, would you explain your theory as to why you would do it this way, over the way i'm doing it. Many thanks,[/quote] When I am using one of CI's libraries and I want to add functionality, I extend it so that the code is all in the same place and it gets loaded automatically when I load the library. I also like to keep most items in static functions instead of stand-alone functions because it does the same thing Namespaces do in other languages, lowers the possibility of name collisions. It also allows you to know that it is associated with. I use an editor with code-assist and it helps that the function only shows up when I am attempting to use that library. |