PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Authentication » LiveUser » Bug #4284

Singleton reference within classes

Details

Request #4284Singleton reference within classes
Submitted2005-05-05 13:14 UTC
Fromtim dot daeleman at euronet dot nl
StatusClosed
PackageLiveUser
PHP Version5.0.3
OSLinux (SuSE 9.1 + Xampp)
Roadmaps(Not assigned)

Comments

[2005-05-05 13:14 UTC] tim dot daeleman at euronet dot nl

Description:
------------
As discussed in the maillist I file this bug to remind you of the following.

I want to use the singleton functionality of LU in my web app. This means I want to instantiate a LiveUser object just once with a correct config array.
Inside classes I want to use this singleton LiveUser object as well, but I don't want to pass the conf array to the classes.

I realise this by instatiating a LU singleton again in the class' constructor which picks up the same instance again I instantiated at the beginning of the program flow with the right config array.
But the singleton method requires at least the config array
But I don't have access to the config array inside the class. Still the targetted functionality is possible by either supressing a warning when not submitting anything or submitting an empty array.

Reproduce code:
---------------
//$conf = full config array

$oLU = LiveUser::singleton($conf);
$oLU->init($username, $password, $logout, $remember);

//check if logged in

if ($oLU->isLoggedIn())
{
//bla bla
}

$oMenu = new topMenu();
$oMenu->createMenu();
$sMenu = $oMenu->getMenu();

//with topMenu class similar to:

class topMenu
{
//(...)

public function __construct()
{
//workaround 1: use empty array
$aEmpty = array();
$this->oLiveUser = LiveUser::singleton($aEmpty);
//workaround 2: suppres warning
$this->oLiveUser = @LiveUser::singleton();
//preferred way to use the method
$this->oLiveUser = LiveUser::singleton();
}
//(...)
public function createMenu()
{
if ($this->oLiveUser->checkRight("MANAGE_USERS"))
{
//create menu item for user management
}
if ($this->LiveUser->checkRight("MANAGE_CONTENT"))
{
//create menu item for content management
}
//etc.
}
//(...)
}

[2005-05-15 23:48 UTC] smith at backendmedia dot com

this might work:

if (!empty($conf)) {
if (empty($instances)) {
return false;
}
$signature = key($instances);
} else {
$signature = serialize(array($handle, $passwd, $confName));
if (!isset($instances[$signature])) {
$obj = &LiveUser::factory(
$conf, $handle, $passwd, $logout, $remember, $confName
);
$instances[$signature] =& $obj;
}
}

[2005-05-16 13:07 UTC] tim dot daeleman at euronet dot nl

Thx for the reactions. I'm happy enough with the workarounds I'm already using. But Lukas asked me to report this bug just to remind him of the fact that this might have to be altered in the code. That's it. So it's up to you to do or not do something with this "bug".

[2005-05-19 04:41 UTC] smith at backendmedia dot com

This bug has been fixed in CVS.

In case this was a documentation problem, the fix will show up at the
end of next Sunday (CET) on pear.php.net.

In case this was a pear.php.net website problem, the change will show
up on the website in short time.

Thank you for the report, and for helping us make PEAR better.