Connecting Tech Pros Worldwide Forums | Help | Site Map

PHP Singleton Function

howachen@gmail.com
Guest
 
Posts: n/a
#1: May 14 '06
hi,

in http://hk.php.net/manual/en/language.oop5.patterns.php, it mentions
the use of "Singleton"function. e.g.

//*********************
public static function singleton()
{
if (!isset(self::$instance)) {
$c = __CLASS__;
self::$instance = new $c;
}

return self::$instance;
}
//*********************

if this instance use a lot of memory, would it be better if i use
reference?

e.g.

//*********************
public static function &singleton()
{
if (!isset(self::$instance)) {
$c = __CLASS__;
self::$instance = new $c;
}

return self::$instance;
}
//*********************

any comments? or the php compiler can auto optimize for me?


Ryan Lange
Guest
 
Posts: n/a
#2: May 14 '06

re: PHP Singleton Function


howachen@gmail.com wrote:[color=blue]
> if this instance use a lot of memory, would it be better if i use
> reference?
>
> e.g.
>
> //*********************
> public static function &singleton()
> {
> if (!isset(self::$instance)) {
> $c = __CLASS__;
> self::$instance = new $c;
> }
>
> return self::$instance;
> }
> //*********************[/color]

By default, objects in PHP5 are passed/returned by reference, so
this is unnecessary.

Ryan
howachen@gmail.com
Guest
 
Posts: n/a
#3: May 14 '06

re: PHP Singleton Function


how about PHP4? need explictly use reference?



Ryan Lange wrote:[color=blue]
> howachen@gmail.com wrote:[color=green]
> > if this instance use a lot of memory, would it be better if i use
> > reference?
> >
> > e.g.
> >
> > //*********************
> > public static function &singleton()
> > {
> > if (!isset(self::$instance)) {
> > $c = __CLASS__;
> > self::$instance = new $c;
> > }
> >
> > return self::$instance;
> > }
> > //*********************[/color]
>
> By default, objects in PHP5 are passed/returned by reference, so
> this is unnecessary.
>
> Ryan[/color]

Mladen Gogala
Guest
 
Posts: n/a
#4: May 15 '06

re: PHP Singleton Function


On Sun, 14 May 2006 11:10:19 -0700, howachen wrote:
[color=blue]
> how about PHP4? need explictly use reference?[/color]

PHP is not Perl. You don't have constructs like \$a and "bless \$a class".
You can explicitly force access by reference by using something like
foreach ($array as &$iterator) but you can't manipulate pointers the way
you do in Perl.

--
http://www.mgogala.com

Ryan Lange
Guest
 
Posts: n/a
#5: May 15 '06

re: PHP Singleton Function


howachen@gmail.com wrote:[color=blue]
> how about PHP4? need explictly use reference?[/color]

Yeah.

Ryan
Oli Filth
Guest
 
Posts: n/a
#6: May 15 '06

re: PHP Singleton Function


howachen@gmail.com said the following on 14/05/2006 19:10:[color=blue]
> Ryan Lange wrote:[color=green]
>> howachen@gmail.com wrote:[color=darkred]
>>> if this instance use a lot of memory, would it be better if i use
>>> reference?
>>>
>>> e.g.
>>>
>>> //*********************
>>> public static function &singleton()
>>> {
>>> if (!isset(self::$instance)) {
>>> $c = __CLASS__;
>>> self::$instance = new $c;
>>> }
>>>
>>> return self::$instance;
>>> }
>>> //*********************[/color]
>> By default, objects in PHP5 are passed/returned by reference, so
>> this is unnecessary.
>>[/color]
> how about PHP4? need explictly use reference?
>[/color]

Somewhat irrelevant, as PHP 4 doesn't implement static class members.


--
Oli
howachen@gmail.com
Guest
 
Posts: n/a
#7: May 31 '06

re: PHP Singleton Function


however, php4 support static variable at the function level

which make Singleton implementation possible, sth like that...

e.g.

fucntion getManager() {

static managerInstance;

if (!isset(managerInstance)) {
managerInstance = new Manager();
}

return managerInstance;

};

....


Oli Filth 寫道:
[color=blue]
> howachen@gmail.com said the following on 14/05/2006 19:10:[color=green]
> > Ryan Lange wrote:[color=darkred]
> >> howachen@gmail.com wrote:
> >>> if this instance use a lot of memory, would it be better if i use
> >>> reference?
> >>>
> >>> e.g.
> >>>
> >>> //*********************
> >>> public static function &singleton()
> >>> {
> >>> if (!isset(self::$instance)) {
> >>> $c = __CLASS__;
> >>> self::$instance = new $c;
> >>> }
> >>>
> >>> return self::$instance;
> >>> }
> >>> //*********************
> >> By default, objects in PHP5 are passed/returned by reference, so
> >> this is unnecessary.
> >>[/color]
> > how about PHP4? need explictly use reference?
> >[/color]
>
> Somewhat irrelevant, as PHP 4 doesn't implement static class members.
>
>
> --
> Oli[/color]

Closed Thread