Re: Need to return reference in this case?



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
.