Constructor Problem - 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: Constructor Problem (/showthread.php?tid=19862) |
Constructor Problem - El Forum - 06-21-2009 [eluser]marty123[/eluser] Hi, I'm trying to implement my own library, there is an constructor like this one: Code: class Menu { But when I try to use $CI in functions, there is an error : A PHP Error was encountered Severity: Notice Message: Undefined variable: CI Filename: libraries/menu.php Line Number: 29 I call my library this way: Code: $this->load->library('menu'); in all functions in the class, but I thought it should take $CI from constructor, am I wrong? Constructor Problem - El Forum - 06-21-2009 [eluser]Dam1an[/eluser] For a start, you mis spelt function for get_menu You need to either make the CI instance a class variable, or create an instance for each function For the first method, use Code: class Menu { Constructor Problem - El Forum - 06-21-2009 [eluser]marty123[/eluser] Thanks, that works. I'm confused that in userguide is: Once you've assigned the object to a variable, you'll use that variable instead of $this: and example Code: $CI =& get_instance(); Thanks again Constructor Problem - El Forum - 06-21-2009 [eluser]Dam1an[/eluser] I'm assuming that's only specific to that function It's just trying to make the point that in most cases (eg controllers you use $this->load etc) but in this case you need to use $CI->load etc If you make the CI instance a class variable, you access it just like any other class variable, ie. $this->CI so it now becoes $this->CI->load etc Clear things up a bit? Constructor Problem - El Forum - 06-22-2009 [eluser]marty123[/eluser] yeah, it's clear, thanks |