You are here: Re: Need to return reference in this case? « PHP Programming Language « IT news, forums, messages
Re: Need to return reference in this case?

Posted by gosha bine on 06/04/07 16:26

On 04.06.2007 17:58 howa wrote:
> On Jun 4, 10:34 pm, gosha bine <stereof...@gmail.com> 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

 

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

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