CodeIgniter Forums
loading a helper in 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: loading a helper in a library (/showthread.php?tid=23432)



loading a helper in a library - El Forum - 10-10-2009

[eluser]tokyotech[/eluser]
I wrote my own DateForm library that creates HTML dropdowns of the months and days. I tried to do $this->load->helper('form') in its constructor but an error was given saying "load" was not defined. I suppose this is because only controllers are allowed to load helpers. But isn't it cumbersome to have to load the form helper every time I want to load my DateForm library?


loading a helper in a library - El Forum - 10-10-2009

[eluser]BrianDHall[/eluser]
Code:
library class...
{
   private $ci;

   function __construct()
   {
       $this->ci =& get_instance();
      
       if (! function_exists(form_open))
       {
           $this-ci->load->helper('form');
       }
   }
}

This should allow your DateForm library to be independent, and will load the form helper on its own if it isn't already loaded.