|
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]
|