Welcome Guest, Not a member yet? Register   Sign In
Library using Singleton
#1

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

[eluser]InsiteFX[/eluser]
Because CodeIgniter supports php4

Enjoy
InsiteFX
#3

[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
#4

[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');

Whatever::something(); // oooh im static!

$thing = new Whatever(); // throw-aways
#5

[eluser]InsiteFX[/eluser]
Code:
public $obj;

private function __construct()
{
    $obj =& get_instance();
}

Enjoy
InsiteFX
#6

[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) {
    CI::$APP->$_alias = new $library($params);
} else {
    CI::$APP->$_alias = call_user_func($library .'::getInstance');
}

Thanks everybody!
#7

[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
        $CI =& get_instance();
        if ($config !== NULL)
        {
            if (isset($config['static']) ? $config['static'] === TRUE : FALSE){
                $CI->$classvar = $name::getInstance($config);
            }else{
                $CI->$classvar = new $name($config);
            }//End If.  Added support for singleton Libraries
        }
        else
        {
                $CI->$classvar = new $name;
        }

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.
#8

[eluser]Phil Sturgeon[/eluser]
You can't have private __construct().
#9

[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
#10

[eluser]Phil Sturgeon[/eluser]
Well how about that. :-)




Theme © iAndrew 2016 - Forum software by © MyBB