Re: Re: Singleton Pattern?
| From: | Stefan Walk | Date: | Fri, 19 Mar 2004 06:43:34 +0000 |
| Subject: | Re: Re: Singleton Pattern? | ||
| References: | 1 2 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-26583@lists.php.net to get a copy of this message | ||
On Fri, Mar 19, 2004 at 12:18:38AM +0100, Stefan Neufeind wrote:
> On 18 Mar 2004 at 23:54, Michael Wallner wrote:
>
> > that's simple because of an PHP4 bug. Either assign
> > the object to $obj by value, or don't use a static
> > method variable for implementing the singleton
> > pattern in your package.
>
> Reply to myself :-))
>
> I've found the corresponding bug report:
> http://bugs.php.net/bug.php?id=12454
>
> It says that "Static variables just can't hold references of any
> kind. For objects it's avoidable with ZE2 because of the new object
> model.". At that time it was marked as a documentation-problem,
> should have been fixed and is marked fixed in CVS (documentation). So
> the problem seems to be of technical nature and maybe isn't fixable
> with the "old" Zend-engine (php 4.x).
It would mean a lot of messing around, i guess. The reason for this
behaviour is this:
What happens when you do static is similar to what happens when you use
global. As you might know,
global $foo;
is equivalent to
$foo =& $GLOBALS['foo'];
So, when you assign a reference to $foo again, you don't modify the
original variable, but make the 'new' one refer to another value.
It's the same with static. You could think of
static $foo;
as a (non-working, just to clarify)
$foo =& $STATICS['function_name']['foo'];
so, when you do $foo =& new Bar; it loses its reference to the static
variable.
>
> Could still somebody please try latest php 5?
>
The behaviour should be the same, however all this meddling with
references is unneccessary and can (and should) be easily avoided in
php5 thanks to the way objects are handled now (variables just contain
'handles' to objects and not objects themselves, so copying them
doesn't hurt anymore.)
>
> Thank you,
> Stefan
>
Hope that helped,
Stefan :)
--
Regards,
Stefan Walk
<swalk@prp.physik.tu-darmstadt.de>