473,939 Members | 2,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Object suicude

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.
Jul 17 '05 #1
12 2151
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 pressBigRedButt on() {
unset($this);
}
}

$test = new Test;
$test->pressBigRedBut ton();

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.andyhsoftwa re.co.uk/space>
Jul 17 '05 #2
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.c om...
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 pressBigRedButt on() {
unset($this);
}
}

$test = new Test;
$test->pressBigRedBut ton();

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.andyhsoftwa re.co.uk/space>

Jul 17 '05 #3
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******* *************@c omcast.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.c om...
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 pressBigRedButt on() {
unset($this);
}
}

$test = new Test;
$test->pressBigRedBut ton();

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.andyhsoftwa re.co.uk/space>

Jul 17 '05 #4
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.andyhsoftwa re.co.uk/space>
Jul 17 '05 #5
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.goo gle.com...
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******* *************@c omcast.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.c om...
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 pressBigRedButt on() {
unset($this);
}
}

$test = new Test;
$test->pressBigRedBut ton();

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.andyhsoftwa re.co.uk/space>

Jul 17 '05 #6
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 pressBigRedButt on() {
$this= new Transmute();
}
}
$test = new Test;
echo $test->x;
$test->pressBigRedBut ton();
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

Jul 17 '05 #7
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.
Jul 17 '05 #8
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

Jul 17 '05 #9
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.
Jul 17 '05 #10

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

Similar topics

6
2472
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 happens if one of those objects, when it is created, takes a reference to the object that contains it? Do bad things happen? class McShow {
15
2304
by: Ville Vainio | last post by:
Pythonic Nirvana - towards a true Object Oriented Environment ============================================================= IPython (by Francois Pinard) recently (next release - changes are still in CVS) got the basic abilities of a system shell (like bash). Actually, using it as a shell is rather comfortable. This all made me think... Why do we write simple scripts to do simple things? Why do we serialize data to flat text files in...
1
3239
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 understand is:
28
20377
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()', this.pinginterval); - but is there no way to do this without using the literal ObjectName? If I write 'this.methodName()' I get "Line 1 Char 1: Object doesn't support this property or method." in IE, and nothing happens in Firebird.
7
1944
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. This object reference is passed into another object create within the same
0
4639
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 I don't understand is:
26
5717
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 several parts of the DOM, but this does not include the window object. Thank you
3
2787
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? http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Defining_Functions doesn't actually give any insight.
2
2670
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);
0
11109
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11289
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10658
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9858
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8218
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7384
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4906
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3501
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.