CodeIgniter Forums
load->library() doesn't support singletons! - 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: load->library() doesn't support singletons! (/showthread.php?tid=4736)



load->library() doesn't support singletons! - El Forum - 12-12-2007

[eluser]sherbo[/eluser]
I'm implementing a few classes in the CodeIgniter "libraries" folder as singletons in the following manner:

Code:
public static function getInstance() {.....}

private function __construct() {}

CodeIgniter's load->library(), however, always expects a public constructor.

Is it possible to implement and load CI library classes as singletons?

Also, what was the logic behind forcing lower case names of variables returned by load->library()?

In general, the CodeIgniter's library loading functionality leaves much to be desired.


load->library() doesn't support singletons! - El Forum - 12-13-2007

[eluser]alexsancho[/eluser]
This should work, you can load the library without run it, and then use your static methods as always

Code:
$this->load->library('yourclass', false);

yourclass::getInstance()->method();



load->library() doesn't support singletons! - El Forum - 12-13-2007

[eluser]sherbo[/eluser]
Hi Alex,

Your idea didn't work. CodeIgniter still complains about trying to call a private constructor.

I found a way to substitue CodeIgniter's anemic load->library() function with standard PHP class loading.

In the root index.php, I added the following line:
Code:
set_include_path(APPPATH.'/libraries/');

Inside a controller, I access my class the regular PHP way:

Code:
require_once myclass;

myclass::getInstance();



load->library() doesn't support singletons! - El Forum - 12-13-2007

[eluser]tonanbarbarian[/eluser]
Ok I wasnt going to get into this thread because I did not want what I said appear to be negative.
So please do not take it that way....

What you are trying to do is just not the way CI works. It does use a public constructor so you should just do the same.
I know you want to use PHP 5 etc but since CI is PHP 4 compatible it is better, in my opinion only, to just work with it the way you have to and move on.


load->library() doesn't support singletons! - El Forum - 12-13-2007

[eluser]alexsancho[/eluser]
Sorry @sherbo, i was talking about load_class function not loader class, my fault. I use this function to load static classes without problem.