Re: unset($this) or maybe unset(&$this)
- From: Oli Filth <catch@xxxxxxxxxxxxxx>
- Date: Thu, 23 Feb 2006 13:16:00 GMT
Tony Marston said the following on 23/02/2006 10:02:
"Oli Filth" <catch@xxxxxxxxxxxxxx> wrote in message news:0F7Lf.59404$mf2.36568@xxxxxxxxxxxxxxxxxxxxxxxTony 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
.
- Follow-Ups:
- Re: unset($this) or maybe unset(&$this)
- From: Tony Marston
- Re: unset($this) or maybe unset(&$this)
- References:
- OOP: unset($this) or maybe unset(&$this)
- From: lievendp
- Re: unset($this) or maybe unset(&$this)
- From: Tony Marston
- Re: unset($this) or maybe unset(&$this)
- From: Oli Filth
- Re: unset($this) or maybe unset(&$this)
- From: Tony Marston
- OOP: unset($this) or maybe unset(&$this)
- Prev by Date: Re: User uploading files to load balanced server cluster
- Next by Date: Linking multiple records from another table
- Previous by thread: Re: unset($this) or maybe unset(&$this)
- Next by thread: Re: unset($this) or maybe unset(&$this)
- Index(es):
Relevant Pages
|