CodeIgniter Forums
Loading a Library with a different Name doesn't work - 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 Library with a different Name doesn't work (/showthread.php?tid=40516)



Loading a Library with a different Name doesn't work - El Forum - 04-11-2011

[eluser]Unknown[/eluser]
Hello there,
my Problem is pretty simple:

I have written my own Form Class and I sometimes need more than one instance of it, with a different name.

Code:
$this->load->library("MY_Form");
$this->my_form->setTargetUrl($targetUrl);
This is working.

Code:
$this->load->library("MY_Form", "", "my_form2");
$this->my_form2->setTargetUrl($targetUrl);
This is not.

It worked fine before Codeigniter 2.0 and now I get "Message: Undefined property: User::$my_form2".
Maybe I'm just too stupid to properly read the documentation, but I don't see a mistake on my side.


Loading a Library with a different Name doesn't work - El Forum - 04-11-2011

[eluser]WanWizard[/eluser]
Libraries are loaded as singletons, they're not supported to support multiple instances. If you want that, simply use "new Libraryname();". Loading it again won't work, it will just return the already loaded instance.

Are you using some kind of modular solution? Because your second code block should work without problems.


Loading a Library with a different Name doesn't work - El Forum - 04-12-2011

[eluser]Unknown[/eluser]
The Lib loading is singleton? Well good to know. Smile

But like I said, that Code worked before 2.0.
And even if it's just returning another instance with a different name, this should still work, right?


I'm not using anything special that I'm aware of.
The Form Library is a normal Class that extends another Library Class.


Thanks for the tip with "new" anyway, because this works:
Code:
$this->my_form2 = new MY_Form();