![]() |
ERROR getting multiple instances of a Library Class in CodeIgniter - 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: ERROR getting multiple instances of a Library Class in CodeIgniter (/showthread.php?tid=60760) |
ERROR getting multiple instances of a Library Class in CodeIgniter - El Forum - 06-23-2014 [eluser]Unknown[/eluser] Hi there guys, I got a categories.php which is my Controller. I also created a treenode.php and put it in my libraries/ folder. Inside my controller I got a function test() (see code below) Code: function test() { The problem is that when I do the print_r($currentTreeNode) I get always the first instance instead of a new one every time. So I suppose it has to be a problem with the creation of Library instances. I get this output: Quote:TreeNode Object ( [_parent:TreeNode:private] => 0 [_children:TreeNode:private] => Array ( ) ) where I would like to get: Quote:TreeNode Object ( [_parent:TreeNode:private] => 0 [_children:TreeNode:private] => Array ( ) ) Here is my categories database table in order to have a more complete view of the problem: [id] name parent_category 1 ROOT 0 2 FOOD 1 3 NONFOOD 1 ERROR getting multiple instances of a Library Class in CodeIgniter - El Forum - 06-24-2014 [eluser]hot_sauce[/eluser] Code: Add this in your helper! ERROR getting multiple instances of a Library Class in CodeIgniter - El Forum - 06-26-2014 [eluser]joergy[/eluser] Because CI instantiates models and libraries for You, it always reuses them if possible. I caught the same pitfall. Therefor I don't use CTORs of Libraries for initialization but my own "init()" function. On the other hand You can use a non documented third parameter, which will become the instance-name. E.a. $this->load->library("libname",$params,"instance_of_lib"); then using it $this-> instance_of_lib->... But then You must somehow track the instance names already used.... ERROR getting multiple instances of a Library Class in CodeIgniter - El Forum - 06-26-2014 [eluser]CroNiX[/eluser] [quote author="joergy" date="1403810159"] On the other hand You can use a non documented third parameter, which will become the instance-name.[/quote] The 3rd parameter for the instance is documented for the loader::library() method. http://ellislab.com/codeigniter/user-guide/libraries/loader.html |