![]() |
Library using Singleton - 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: Library using Singleton (/showthread.php?tid=28808) |
Library using Singleton - El Forum - 03-22-2010 [eluser]Thiago Festa[/eluser] Hello everybody, So, I'm trying to create a library called User, that is a Singleton from current user. But I can't put the private function __construct() on this library, coz it get an error. Do you know why it happening? Thanks Library using Singleton - El Forum - 03-22-2010 [eluser]InsiteFX[/eluser] Because CodeIgniter supports php4 Enjoy InsiteFX Library using Singleton - El Forum - 03-23-2010 [eluser]Thiago Festa[/eluser] Yes, it doesn't matter... I'm using PHP 5, so I'm talking about Singleton, and CI uses singleton in other piece of code, coz i saw some methods called get_instance() in several core files. So I need some help to create a library using singleton, coz i'll use it on user administration. By the way, the private function __construct got an error, coz the method library ($this->load->library("test_singleton")) put a new Test_singleton(), so it needs to have a way to call the getInstance() instead of try to create a new instance. Thanks a lot Library using Singleton - El Forum - 03-23-2010 [eluser]Phil Sturgeon[/eluser] When a library is loaded it will be instantiated straight away. After that you can use it normally as you would in native PHP 4 or 5. Code: $this->load->library('whatever'); Library using Singleton - El Forum - 03-23-2010 [eluser]InsiteFX[/eluser] Code: public $obj; Enjoy InsiteFX Library using Singleton - El Forum - 03-23-2010 [eluser]Thiago Festa[/eluser] Fixed! So, i'm using HMVC, and i just change the method library on Loader class on Controller.php. I put another param, called "$instanciate = TRUE", when you put false there, i use singleton. Look for my new code: Code: if($instanciate) { Thanks everybody! Library using Singleton - El Forum - 05-15-2010 [eluser]ketema[/eluser] I was looking for a way to do the same thing: have singleton libraries. This was the only post that I could find on the subject, but the "fix" didn't make sense, perhaps I have a different version of CI. Anyway here is how I got it to work: CI Version 1.7.2 system/libraries/Loader.php inside function _ci_init_class, under comment //Instantiate the class Code: // Instantiate the class I did my best to make it as unobtrusive as possible so as not to mess up anything else. The singleton library is now loaded with Code: $this->ci->load->library('singleton', array('static' => TRUE)); You can still pass in other parameters, and the 'static' index of the params array is not required for other libraries so it won't break other load library statements. I thought about not hard coding the "getInstance" name and allowing the user to pass in whatever they wanted to get the singleton, but all my reading suggested getInstance was a pretty standard name for the caller function. I hope this is useful to others. Library using Singleton - El Forum - 05-18-2010 [eluser]Phil Sturgeon[/eluser] You can't have private __construct(). Library using Singleton - El Forum - 05-18-2010 [eluser]ketema[/eluser] [quote author="Phil Sturgeon" date="1274192148"]You can't have private __construct().[/quote] Yes you can. http://php.net/manual/en/language.oop5.decon.php All it does is prevent your object from being created with the new keyword. Which is the entire point of a singleton. http://en.wikipedia.org/wiki/Singleton_pattern Library using Singleton - El Forum - 05-18-2010 [eluser]Phil Sturgeon[/eluser] Well how about that. :-) |