Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old May 14th, 2006, 04:45 PM
howachen@gmail.com
Guest
 
Posts: n/a
Default PHP Singleton Function

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?

  #2  
Old May 14th, 2006, 06:15 PM
Ryan Lange
Guest
 
Posts: n/a
Default 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
  #3  
Old May 14th, 2006, 07:15 PM
howachen@gmail.com
Guest
 
Posts: n/a
Default 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]

  #4  
Old May 15th, 2006, 04:25 AM
Mladen Gogala
Guest
 
Posts: n/a
Default 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

  #5  
Old May 15th, 2006, 07:35 PM
Ryan Lange
Guest
 
Posts: n/a
Default Re: PHP Singleton Function

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

Yeah.

Ryan
  #6  
Old May 15th, 2006, 07:45 PM
Oli Filth
Guest
 
Posts: n/a
Default 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
  #7  
Old May 31st, 2006, 06:35 PM
howachen@gmail.com
Guest
 
Posts: n/a
Default 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]

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,338 network members.