Reply to Re: unset($this) or maybe unset(&$this)

Your name:

Reply:


Posted by Oli Filth on 02/23/06 15:16

Tony Marston said the following on 23/02/2006 10:02:
> "Oli Filth" <catch@olifilth.co.uk> wrote in message
> news:0F7Lf.59404$mf2.36568@newsfe6-win.ntli.net...
>> Tony Marston said the following on 21/02/2006 09:46:
>>> Don't be silly. The execution path is sequential, so after a command
>>> which does not involve a jump is executed the next instruction to be
>>> executed will be the very next instruction in the same object method. If
>>> you have just erased the object that contains the next instruction what
>>> do you think will happen? How is the PHP processor supposed to know where
>>> to go?
>>>
>> I dunno about PHP, but the equivalent is perfectly possible in C++, i.e.
>> you can call delete on this (assuming it's a heap-based object).
>>
>> The object and its method code are not one and the same thing. Deleting an
>> object doesn't mean that the code disappears...
>
> An object is comprised of methods (code) and properties (variables). If you
> delete/unset an object then both disappear as all their reference points no
> longer exist. The code may still exist in the class definition, but the
> object, which contains a copy of that code in memory, does not, so how can
> it continue executing any of that code?

Well, in C++, objects most definitely don't keep a copy of the method
code in memory. When you call something like obj.Func(var); in C++
(assuming obj is an instance of class Foo), the compiler actually
translates that to something like:

__Foo__Func(&obj, var);

with the method actually just equivalent to a normal global function,
internally defined as something like:

void __Foo__Func(struct Foo *this, int var)
{
...
}

and Foo internally defined as:

struct Foo
{
/* member variables of Foo */
}

Calling delete this; just deallocates the storage space set aside for
the Foo struct. Nothing at all happens to the code.

I'd like to think that something similar occurs in PHP, as making a
"copy" of the code every time you create an object would be a waste of
time and memory.


> Deleting an object while you are still inside it is like blowing up a house
> when you are still inside - not a good idea!

Going back to C++, use of "delete this" is quite a common practice in
smart-pointer/reference-counting classes, or objects designed not to
have any other references to them.


--
Oli

[Back to original 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

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