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



Tony Marston said the following on 23/02/2006 10:02:
"Oli Filth" <catch@xxxxxxxxxxxxxx> wrote in message news:0F7Lf.59404$mf2.36568@xxxxxxxxxxxxxxxxxxxxxxx
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
.



Relevant Pages

  • Re: Accessing program counter registers from within C or Assembler.
    ... >> current executing instruction in C while the ... it is close to the second scenario ("Where in memory ... is there a method to load a program image ...
    (Linux-Kernel)
  • Re: Revise text section that has been loaded into virtual memory
    ... > the executable that has been loaded into memory. ... >between all processes executing the same code. ... >instruction and data caches and any write operation goes to the data cache ... >while any instruction fetch will be done through the instruction cache. ...
    (comp.os.linux.development.system)
  • Re: question about nonaligned memory access
    ... three bits, the tag of the object, are 001, the tag for a cons cell. ... When the function foo is being compiled in safe code, ... which was the address of the instruction that did the ... handled by a signal handler. ...
    (comp.arch)
  • Re: Cross-Modifying Code
    ... > There's an explicit warning that instruction fetch and page table accesses ... > don't honour xFENCE for prefetched code either. ... > then you don't know when to issue the CPUID. ... The Opteron manuals don't comment on the interaction between executing code ...
    (comp.lang.asm.x86)
  • Re: Question about Instruction Format (ModR/M)
    ... (the first three bytes, being prefixes, could be in any order) ... This is a 32-bit PM instruction executing in 16-bit RM/PM due to ... they have to push the extra byte back onto the disassembly stream. ...
    (alt.lang.asm)