Welcome Guest, Not a member yet? Register   Sign In
load->library() doesn't support singletons!
#1

[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.
#2

[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();
#3

[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();
#4

[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.
#5

[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.




Theme © iAndrew 2016 - Forum software by © MyBB