|
Posted by gosha bine on 06/04/07 14:34
On 04.06.2007 15:31 howa wrote:
> On Jun 3, 6:42 pm, gosha bine <stereof...@gmail.com> wrote:
>> howa wrote:
>>> On 6 3 , 6 42 , gosha bine <stereof...@gmail.com> wrote:
>>>> howa wrote:
>>>> $foo = new Foo
>>>> does the following in php4:
>>>> 1) a chunk of memory is allocated and object's constructor is called to
>>>> initialize this memory
>>>> 2) another chunk of memory is allocated and everything from the first
>>>> chunk is copied to it
>>>> 3) variable 'foo' is being set to address of this second chunk
>>>> 4) the first chunk is being disposed.
>>>> Apart from being ineffective, this technique will apparently fail if an
>>>> object saves reference to itself ($this) in constructor or when you need
>>>> exactly one instance.
>>> Hello,
>>> Yes, but refer to my original question, both (1) & (2) have this
>>> problem of inefficieny.
>>> So you said (2) is perfferred?
>>> Why? Since PHP said return by reference is mostly useless.
>> function getFoo()...
>> $foo = Foo::getFoo();
>>
>> involves copying as I said above
>>
>> function &getFoo()..
>> $foo =& Foo::getFoo();
>>
>> does not involve copying, only one memory block is created
>>
>
> Good, back to the real problem!
And the real problem is.......?
> but according to PHP, the opimizer is smart to handle this?
to handle what?
>
>>> Do not use return-by-reference to increase performance, the engine is smart enough to optimize this on its own
please also read the NEXT phrase ;)
>
> http://hk.php.net/manual/en/language.references.return.php
>
> any comments?
>
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.
--
gosha bine
extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
[Back to original message]
|