Re: Need to return reference in this case?
- From: gosha bine <stereofrog@xxxxxxxxx>
- Date: Mon, 04 Jun 2007 18:26:45 +0200
On 04.06.2007 17:58 howa wrote:
On Jun 4, 10:34 pm, gosha bine <stereof...@xxxxxxxxx> wrote:
Yet again, "$a = new Foo" actually creates TWO objects in php4. The
first one is an anonymous object created by "new" and the second one is
the object the variable "$a" points to. This behavior is ineffective AND
it doesn't work in certain cases, e.g singletons.
but using a static function variable made singleton possible (in
PHP4), as my example above, both ways are working...
Just test it
- - - - - - - -
class Foo {
var $x;
function /*&*/getFoo() {
static $instance;
if (!isset($instance))
$instance = new Foo();
return $instance;
}
}
$a = /*&*/Foo::getFoo();
$b = /*&*/Foo::getFoo();
$a->x = 123;
# $b->x should be 123 too
# because it's a SINGLETON
assert($b->x == 123); // FAILURE
- - - - - - - -
Run it. Did it fail? Do you understand why?
Now, uncomment all references and try again.
--
gosha bine
extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
.
- References:
- Need to return reference in this case?
- From: howa
- Re: Need to return reference in this case?
- From: gosha bine
- Re: Need to return reference in this case?
- From: howa
- Re: Need to return reference in this case?
- From: gosha bine
- Re: Need to return reference in this case?
- From: howa
- Re: Need to return reference in this case?
- From: gosha bine
- Re: Need to return reference in this case?
- From: howa
- Re: Need to return reference in this case?
- From: gosha bine
- Re: Need to return reference in this case?
- From: howa
- Need to return reference in this case?
- Prev by Date: Efficiency of mysql_close()
- Next by Date: Re: Efficiency of mysql_close()
- Previous by thread: Re: Need to return reference in this case?
- Next by thread: Re: Need to return reference in this case?
- Index(es):