Is there a way for an object to unset itself in PHP4?
Of course I could unset it from outside, but I would like to keep the code
clean. The object has a method which deletes it's "footprint" in the
database, and as the idea is that we don't need the object once itsin't in
the DB it would be nice if the same method could get rid of the PHP object
iself as well.
Berislav
--
If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
Groucho, Chico, and Harpo, then Usenet is Zeppo. 12 2108
On Thu, 5 Feb 2004 17:25:28 +0100, "Berislav Lopac" <be************@dimedia.hr>
wrote: Is there a way for an object to unset itself in PHP4?
Of course I could unset it from outside, but I would like to keep the code clean. The object has a method which deletes it's "footprint" in the database, and as the idea is that we don't need the object once itsin't in the DB it would be nice if the same method could get rid of the PHP object iself as well.
The obvious idea of using unset($this) doesn't appear to work:
<?php
class Test
{
var $x = 'x';
function pressBigRedButton() {
unset($this);
}
}
$test = new Test;
$test->pressBigRedButton();
echo $test->x;
?>
(outputs 'x', rather than a warning).
--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
<http://www.andyh.co.uk> / <http://www.andyhsoftware.co.uk/space>
unset($this) only remove the variable "this" from the current namespace.
What you want to do is $this = null. That sets what $this was pointing to to
null.
Uzytkownik "Andy Hassall" <an**@andyh.co.uk> napisal w wiadomosci
news:c5********************************@4ax.com... On Thu, 5 Feb 2004 17:25:28 +0100, "Berislav Lopac"
<be************@dimedia.hr> wrote:
Is there a way for an object to unset itself in PHP4?
Of course I could unset it from outside, but I would like to keep the
codeclean. The object has a method which deletes it's "footprint" in the database, and as the idea is that we don't need the object once itsin't
inthe DB it would be nice if the same method could get rid of the PHP
objectiself as well.
The obvious idea of using unset($this) doesn't appear to work:
<?php class Test { var $x = 'x'; function pressBigRedButton() { unset($this); } }
$test = new Test; $test->pressBigRedButton();
echo $test->x; ?>
(outputs 'x', rather than a warning).
-- Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool <http://www.andyh.co.uk> / <http://www.andyhsoftware.co.uk/space>
Have a question about $this = null:
In PHP is this its equivalent of destroying an object, say, if you
will, Garbage Collection? I am working on a project that has anywhere
between 10 - 50 different instances occurring at one time (I kid you
not!) and realize this will turn into a horrific bottleneck unless
there is some means of garbage collection.
Thanx
Phil
"Chung Leong" <ch***********@hotmail.com> wrote in message news:<vM********************@comcast.com>... unset($this) only remove the variable "this" from the current namespace. What you want to do is $this = null. That sets what $this was pointing to to null.
Uzytkownik "Andy Hassall" <an**@andyh.co.uk> napisal w wiadomosci news:c5********************************@4ax.com... On Thu, 5 Feb 2004 17:25:28 +0100, "Berislav Lopac" <be************@dimedia.hr> wrote:
Is there a way for an object to unset itself in PHP4?
Of course I could unset it from outside, but I would like to keep the codeclean. The object has a method which deletes it's "footprint" in the database, and as the idea is that we don't need the object once itsin't inthe DB it would be nice if the same method could get rid of the PHP objectiself as well.
The obvious idea of using unset($this) doesn't appear to work:
<?php class Test { var $x = 'x'; function pressBigRedButton() { unset($this); } }
$test = new Test; $test->pressBigRedButton();
echo $test->x; ?>
(outputs 'x', rather than a warning).
-- Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool <http://www.andyh.co.uk> / <http://www.andyhsoftware.co.uk/space>
On Thu, 5 Feb 2004 18:16:47 -0500, "Chung Leong" <ch***********@hotmail.com>
wrote: unset($this) only remove the variable "this" from the current namespace. What you want to do is $this = null. That sets what $this was pointing to to null.
Ah - thanks, I wasn't aware of the distinction. Looks like this is what Phil
is looking for, as a quick test appears to show it does destroy the object.
--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
<http://www.andyh.co.uk> / <http://www.andyhsoftware.co.uk/space>
In theory, yes. In practice, I have not a clue. On my server the Apache
process regularly chew up half a gig of memory.
Uzytkownik "Phil Powell" <so*****@erols.com> napisal w wiadomosci
news:1c**************************@posting.google.c om... Have a question about $this = null:
In PHP is this its equivalent of destroying an object, say, if you will, Garbage Collection? I am working on a project that has anywhere between 10 - 50 different instances occurring at one time (I kid you not!) and realize this will turn into a horrific bottleneck unless there is some means of garbage collection.
Thanx Phil
"Chung Leong" <ch***********@hotmail.com> wrote in message
news:<vM********************@comcast.com>... unset($this) only remove the variable "this" from the current namespace. What you want to do is $this = null. That sets what $this was pointing
to to null.
Uzytkownik "Andy Hassall" <an**@andyh.co.uk> napisal w wiadomosci news:c5********************************@4ax.com... On Thu, 5 Feb 2004 17:25:28 +0100, "Berislav Lopac" <be************@dimedia.hr> wrote:
>Is there a way for an object to unset itself in PHP4? > >Of course I could unset it from outside, but I would like to keep the code >clean. The object has a method which deletes it's "footprint" in the >database, and as the idea is that we don't need the object once
itsin't in >the DB it would be nice if the same method could get rid of the PHP object >iself as well.
The obvious idea of using unset($this) doesn't appear to work:
<?php class Test { var $x = 'x'; function pressBigRedButton() { unset($this); } }
$test = new Test; $test->pressBigRedButton();
echo $test->x; ?>
(outputs 'x', rather than a warning).
-- Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool <http://www.andyh.co.uk> / <http://www.andyhsoftware.co.uk/space>
Andy Hassall writes: On Thu, 5 Feb 2004 18:16:47 -0500, "Chung Leong" <ch***********@hotmail.com> wrote:
unset($this) only remove the variable "this" from the current namespace. What you want to do is $this = null. That sets what $this was pointing to to null.
Ah - thanks, I wasn't aware of the distinction. Looks like this is what Phil is looking for, as a quick test appears to show it does destroy the object.
Well that is interesting. I had to try it myself. Conceptually $this
is just another parameter passed by reference, and you can do anything
you want to it, including assigning it a completely different object.
I adapted the suicide test to do this -
<?php
class Transmute {
var $y = 'y';
}
class Test {
var $x = 'x';
function pressBigRedButton() {
$this= new Transmute();
}
}
$test = new Test;
echo $test->x;
$test->pressBigRedButton();
echo $test->x;
echo $test->y;
?>
Returns
x
Notice: Undefined property: x in suicide.php on line 16
y
--
__o Alex Farran
_`\<,_ Analyst / Programmer
(_)/ (_) www.alexfarran.com
Phil Powell wrote: Have a question about $this = null:
In PHP is this its equivalent of destroying an object, say, if you will, Garbage Collection? I am working on a project that has anywhere between 10 - 50 different instances occurring at one time (I kid you not!) and realize this will turn into a horrific bottleneck unless there is some means of garbage collection.
There's none in PHP4. Even if you do $someObject = null or unset() or
anything else it will not free up memory used by that object. Memory is
only freed up when the script execution ends.
This only applies to objects and not other types like arrays, strings, etc.
Zurab Davitiani writes: Phil Powell wrote: Have a question about $this = null:
In PHP is this its equivalent of destroying an object, say, if you will, Garbage Collection? I am working on a project that has anywhere between 10 - 50 different instances occurring at one time (I kid you not!) and realize this will turn into a horrific bottleneck unless there is some means of garbage collection.
There's none in PHP4. Even if you do $someObject = null or unset() or anything else it will not free up memory used by that object. Memory is only freed up when the script execution ends.
What makes you think that? PHP4's garbage collection algorithm is an
unsophisticated reference counter, so it may not be the most efficient
in the world, but I'd be very surprised if objects weren't GC'd.
--
__o Alex Farran
_`\<,_ Analyst / Programmer
(_)/ (_) www.alexfarran.com
Alex Farran wrote: There's none in PHP4. Even if you do $someObject = null or unset() or anything else it will not free up memory used by that object. Memory is only freed up when the script execution ends.
What makes you think that? PHP4's garbage collection algorithm is an unsophisticated reference counter, so it may not be the most efficient in the world, but I'd be very surprised if objects weren't GC'd.
I can't see how they are. I've tried few scripts quite a few times and
different ways that create new objects in a for-loop. Memory consumption
never goes down, but only increases as the script continues to run.
Eventually, the available memory is used up and you either get a fatal
error (unable to allocate memory), or the process simply crashes, depending
on your version of PHP4.
This only happens with objects, but not with other types, as I said; even if
you try to unset or =null or reassign the same variable name to another
object - doesn't matter how you do it.
I'd be interested to see what method you are suggesting to use that will
properly destroy the object, freeing the associated memory. It just doesn't
happen automatically with the garbage collection like with the other types
of variables.
Zurab Davitiani writes: I'd be interested to see what method you are suggesting to use that will properly destroy the object, freeing the associated memory. It just doesn't happen automatically with the garbage collection like with the other types of variables.
Well I was arguing from my understanding of the garbage collection
mechanism rather than practical experience, but I ran simple test that
proves that objects are garbage collected just like any other block of
memory. It wouldn't be a very practical object system if they
weren't.
The PHP4 garbage collector uses a reference counting algorithm. What
this means is that any block of memory, be it an array, object, string
or integer, is freed when there are no references to it. This works
well for simple cases, but can be fooled by cyclic references. If two
objects reference each other, or one object references itself, then
the reference count will always be more than 0 so the object(s) will
never be GC'd.
Here are my tests -
This script will run to completion, since each time a new object is
created the reference to the previous object is destroyed.
echo "start\n";
for ($i = 1; $i<100000; $i++ ) {
$mem = new memory();
}
echo "end\n";
This script will run out of memory because the reference to each
object is retained.
echo "start\n";
for ($i = 1; $i<100000; $i++ ) {
$mem[$i] = new memory();
}
echo "end\n";
This will also fail because each object contains a reference to
itself.
echo "start\n";
for ($i = 1; $i<100000; $i++ ) {
$mem =& new memory();
$mem->a =& $mem;
}
echo "end\n";
--
__o Alex Farran
_`\<,_ Analyst / Programmer
(_)/ (_) www.alexfarran.com
Hi Berislav!
On Thu, 5 Feb 2004 17:25:28 +0100, "Berislav Lopac"
<be************@dimedia.hr> wrote: Is there a way for an object to unset itself in PHP4?
Of course I could unset it from outside, but I would like to keep the code clean. The object has a method which deletes it's "footprint" in the database, and as the idea is that we don't need the object once itsin't in the DB it would be nice if the same method could get rid of the PHP object iself as well.
Berislav
I have been loking at PostNuke recently and they include everything in
a function. Though they don't use objects, everything is gone and out
of scope, once the function ends.
HTH, Jochen
--
Jochen Daum - Cabletalk Group Ltd.
PHP DB Edit Toolkit -- PHP scripts for building
database editing interfaces. http://sourceforge.net/projects/phpdbedittk/
Alex Farran wrote: Well I was arguing from my understanding of the garbage collection mechanism rather than practical experience, but I ran simple test that proves that objects are garbage collected just like any other block of memory. It wouldn't be a very practical object system if they weren't.
The PHP4 garbage collector uses a reference counting algorithm. What this means is that any block of memory, be it an array, object, string or integer, is freed when there are no references to it. This works well for simple cases, but can be fooled by cyclic references. If two objects reference each other, or one object references itself, then the reference count will always be more than 0 so the object(s) will never be GC'd.
Here are my tests -
This script will run to completion, since each time a new object is created the reference to the previous object is destroyed.
echo "start\n"; for ($i = 1; $i<100000; $i++ ) { $mem = new memory(); } echo "end\n";
This script will run out of memory because the reference to each object is retained.
echo "start\n"; for ($i = 1; $i<100000; $i++ ) { $mem[$i] = new memory(); } echo "end\n";
This will also fail because each object contains a reference to itself.
echo "start\n"; for ($i = 1; $i<100000; $i++ ) { $mem =& new memory(); $mem->a =& $mem; } echo "end\n";
This doesn't seem to work on all objects; and I am confused that it works on
some but not others. I was trying to determine exactly what objects have to
contain for PHP GC not to be able to release the memory, but I was unable
to create a simple test case scenario in a limited free time I had. I will
research it more, and hopefully have more details, and probably start
another thread when I do. Thanks for the info! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: lawrence |
last post by:
How dangerous or stupid is it for an object to have a reference to the
object which contains it? If I have a class called $controllerForAll
which has an arrray of all the objects that exist, what...
|
by: Ville Vainio |
last post by:
Pythonic Nirvana - towards a true Object Oriented Environment
=============================================================
IPython (by Francois Pinard) recently (next release - changes are...
|
by: Bijay Kumar |
last post by:
Hi Guys,
I was going through the source code of Object.cs in rotor. What I found is
Equals() implemented as follows:
public extern virtual bool Equals(Object obj);
What I don't...
|
by: Daniel |
last post by:
Hello =)
I have an object which contains a method that should execute every x ms. I
can use setInterval inside the object construct like this -
self.setInterval('ObjectName.methodName()',...
|
by: Nick Zdunic |
last post by:
I have a remotable object running in my host application.
The host starts up and creates the object. Within a method to start the
remote object doing its thing it creates an object.
...
|
by: Bijay Kumar |
last post by:
Hi Guys,
I was going through the source code of Object class (Object.cs in rotor). What I found is
Equals() implemented as follows:
public extern virtual bool Equals(Object obj);
What...
|
by: yb |
last post by:
Hi,
Is there a standard for the global 'window' object in browsers? For
example, it supports methods such as setInterval and clearInterval, and
several others.
I know that w3c standardized...
|
by: User1014 |
last post by:
A global variable is really just a property of the "Global Object", so
what does that make a function defined in the global context? A method
of the Global Object?
...
|
by: Ralph |
last post by:
Hi
I don't understand why it's not working:
function schedule(imTop){
this.tdImagesTop = imTop;
}
schedule.prototype.selectEl = function() {
alert(this.tdImagesTop);
|
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...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
|
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...
| |