Connecting Tech Pros Worldwide Help | Site Map

PHP Singleton Function

  #1  
Old May 14th, 2006, 04:45 PM
howachen@gmail.com
Guest
 
Posts: n/a
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

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

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

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

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

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
OOP database tables <-> php interface (semi LONG) amygdala answers 22 April 16th, 2007 11:05 PM
I need $this->self to be identical to and reference to $this, but how in PHP 5.2+? comp.lang.php answers 7 January 9th, 2007 04:05 PM
Singleton [Mystic] answers 11 July 17th, 2005 11:49 AM
How do you build a singleton class in PHP? Can you? Phil Powell answers 1 July 17th, 2005 04:18 AM