You are here: Re: Singleton design pattern relveance in PHP « All PHP « IT news, forums, messages
Re: Singleton design pattern relveance in PHP

Posted by Oli Filth on 10/11/33 11:59

Bit Byte said the following on 29/09/2006 12:13:
> ZeldorBlat wrote:
>
>> <?php
>> //Thread starts
>>
>> $a = new Foo();
>> $b = new Foo();
>>
>> //Now have two instances of Foo, all in the same thread -- no
>> guarantees here
>>
>> //Thread ends
>> ?>
>>
>
> Hmm, yes, that was a rather obvious refudiation of the point I was
> making. I obviously wasn't making myself clear. What I meant was to say
> was that other than in cases (such as the one above) where the user
> deliberately creates multiple instances of the same class, I can't see
> the relevance of porting this Pattern to PHP - since each request is
> handled in a new thread/(possibly) process.

If you're not the only user of your class, then the example code above
is entirely possible. Remember that the whole point of a lot of OOP
semantics is purely to enforce design rules - e.g. public vs. private,
abstract, final, etc. In other words, forcing your users to use your
code as it was designed.

Another scenario might be something like the following contrived scenario:

$objects = array();

foreach ($requests as $item)
{
switch ($item)
{
case ITEM_FOO:
$objects[] = Foo::createFoo();
break;

case ITEM_BAR:
$objects[] = Bar::createBar();
break;
}
}
}

Where $requests is an array containing "requests" for creation of Foo
and Bar objects, which are then collected in the $objects array. In
this scenario, if we only want a single shared instance of Bar, for
instance, we would need to design it as a singleton.

Of course this is contrived, but it demonstrates that situations where
multiple requests for an instance are not necessarily as simple as
ZeldorBlat's example above.


--
Oli

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация