473,325 Members | 2,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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

Just wondering,

Is it possible for an instance of a class to unset itself?
can I do "unset($this)" or "unset(&$this)"

regards,
Lieven
http://eye.cc php newsgroups
Feb 21 '06 #1
7 5213
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?

--
Tony Marston

http://www.tonymarston.net

"lievendp" <li******@pandora-dot-be.no-spam.invalid> wrote in message
news:dt***********@news3.infoave.net...
Just wondering,

Is it possible for an instance of a class to unset itself?
can I do "unset($this)" or "unset(&$this)"

regards,
Lieven
http://eye.cc php newsgroups

Feb 21 '06 #2
No, unset($this) will only remove $this from variable space of the
current function scope. The object will remain in the caller scope.

You can do a $this = null to overwrite the object however.

Feb 21 '06 #3
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...

However, like I said, I don't know what PHP allows you to do or not.
--
Oli
Feb 23 '06 #4

"Oli Filth" <ca***@olifilth.co.uk> wrote in message
news:0F*******************@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?

Deleting an object while you are still inside it is like blowing up a house
when you are still inside - not a good idea!
Feb 23 '06 #5
Tony Marston said the following on 23/02/2006 10:02:
"Oli Filth" <ca***@olifilth.co.uk> wrote in message
news:0F*******************@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
Feb 23 '06 #6
PHP is not C++, so expecting the same behaviour from two different languages
is just being too optimistic. It is not wise to delete an object while you
are still inside it, nor is it wise to overwrite it with something else
while you are still inside it. Why? Because it buggers up any references to
the original object which may still exist in other places.

--
Tony Marston
http://www.tonymarston.net

"Oli Filth" <ca***@olifilth.co.uk> wrote in message
news:kA*******************@newsfe5-gui.ntli.net...
Tony Marston said the following on 23/02/2006 10:02:
"Oli Filth" <ca***@olifilth.co.uk> wrote in message
news:0F*******************@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

Feb 24 '06 #7
Tony Marston said the following on 23/02/2006 23:49:
PHP is not C++, so expecting the same behaviour from two different languages
is just being too optimistic.
I'm not; I was just disputing the assertion that an object is a
collection of code and variables.

It is not wise to delete an object while you
are still inside it, nor is it wise to overwrite it with something else
while you are still inside it. Why? Because it buggers up any references to
the original object which may still exist in other places.


In most situations, yes. But the whole point of having an object delete
itself (in OO languages in general) is in situations where there are
intentionally no external pointers/references to it. In PHP, I can't
see how such a situation could exist, though.

The point is moot, however, because the language doesn't allow it...
--
Oli
Feb 24 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Robert Schott | last post by:
Hi .. this is for sure the 1k question on fopen but maybe can someone tell me this weird staff if i'm writing: $H = fopen($value,"r"); while(!feof($H)) { $string = fgets($H,1024); .... .......
12
by: sathia | last post by:
Hi, I have this table:   CREATE TABLE `osservatorio` (   `id` int(10) unsigned NOT NULL auto_increment,   `testo` varchar(255) NOT NULL default '',   `parent` int(11) default NULL,...
3
by: Ron Guerin | last post by:
The New York Linux User's Group invites you to a special presentation by Alex Martelli of Google, on the Python Object Model. This presentation will be held at P.J. Clarke's Sidecar, rather than...
0
by: Ron Guerin | last post by:
(date and time inadvertently omitted last time. sorry!) The New York Linux User's Group invites you to a special presentation by Alex Martelli of Google, on the Python Object Model. This...
84
by: Bibby | last post by:
Hi, I'm interested in getting started in the programming world. I've dabbled in C, C++ and VB6. Which would be the best language to focus my attention to regarding the following considerations: ...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
32
by: Joe | last post by:
I am just starting to use Object Oriented PHP coding, and I am seeing quite often the following (this example taken from a wiki): $wakka =& new Wakka($wakkaConfig); What exactly is the =&, and...
7
by: jeddiki | last post by:
Hi, As I am in Turkey at present, I can not see vidoes on youtube. So I have tried a few proxies but keep finding them slow or not working. So I have installed myphpProxy on my server under...
4
by: mattehz | last post by:
Hey there, I am trying to upload old source files and came across these errors: Warning: Invalid argument supplied for foreach() in /home/mattehz/public_html/acssr/trunk/inc_html.php on line 59...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.