473,608 Members | 2,689 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

destroy referenced objects

Hello,

pretend some noob has (in a fake-static class) provided the following method

public static kill_object($ob j)
{

if (!is_object($ob j))
return false;

$obj = null;

return true;

}

Obviously noob is heading at destroying an object!

Does he succeed?

Let's see:

<code>

$blubber = new crazy_object... ;

... some schnickschnack. ..

if (!static_class: :kill_object($b lubber))
exit(0);

print_r($blubbe r);

if (isset($blubber ))
print "I'm Blubber\n";
else
print "I'm Blubber no more\n";
</code>

noob would expect to see no print_r-output and the message should be

"I'm Blubber no more"

Actually, things are quite different:

1st: Object is printed per print_r

2nd: Message is "I'm Blubber"

Haven't I just killed the object...? Objects, when supplied as parameter,
are passed by reference in php5, aren't they?

OK, that obviously did not work (somehow... - remember, I'm a noob).

I reformulate the static kill_object-method:

public static kill_object(&$o bj)
{

if (!is_object($ob j))
return false;

$obj = null;

return true;

}

Note, that there comes the old (deprecated) reference-operator '&' (that -
for objects - is no longer needed in php5, right?)

This time, however, the result is:

1st: Object is no longer printed (per print_r)

2nd: Message is "I'm Blubber no more"

Please assist!

cheers

juergen

Jul 10 '08 #1
4 2024
Juergen-Bernhard Adler wrote:
Hello,

pretend some noob has (in a fake-static class) provided the following method

public static kill_object($ob j)
{

if (!is_object($ob j))
return false;

$obj = null;

return true;

}

Obviously noob is heading at destroying an object!

Does he succeed?

Let's see:

<code>

$blubber = new crazy_object... ;

... some schnickschnack. ..

if (!static_class: :kill_object($b lubber))
exit(0);

print_r($blubbe r);

if (isset($blubber ))
print "I'm Blubber\n";
else
print "I'm Blubber no more\n";
</code>

noob would expect to see no print_r-output and the message should be

"I'm Blubber no more"

Actually, things are quite different:

1st: Object is printed per print_r

2nd: Message is "I'm Blubber"

Haven't I just killed the object...? Objects, when supplied as parameter,
are passed by reference in php5, aren't they?

OK, that obviously did not work (somehow... - remember, I'm a noob).

I reformulate the static kill_object-method:

public static kill_object(&$o bj)
{

if (!is_object($ob j))
return false;

$obj = null;

return true;

}

Note, that there comes the old (deprecated) reference-operator '&' (that -
for objects - is no longer needed in php5, right?)

This time, however, the result is:

1st: Object is no longer printed (per print_r)

2nd: Message is "I'm Blubber no more"

Please assist!

cheers

juergen

First of all, static methods don't work on objects - they work on the class.

But PHP will destroy an object sometime after the last reference to the
object is removed. If this is your ONLY reference to the object, at
some time, when the PHP garbage collector runs, the object will be deleted.

But this isn't necessarily the only reference to the object, and the
garbage collector won't necessarily run immediately.

It's not like C++ or SmallTalk, where the destructor can destroy an
object immediately.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===

Jul 10 '08 #2
Jerry Stuckle wrote:
Juergen-Bernhard Adler wrote:
>Hello,

pretend some noob has (in a fake-static class) provided the following
method

public static kill_object($ob j)
{

if (!is_object($ob j))
return false;

$obj = null;

return true;

}

Obviously noob is heading at destroying an object!

Does he succeed?

Let's see:

<code>

$blubber = new crazy_object... ;

... some schnickschnack. ..

if (!static_class: :kill_object($b lubber))
exit(0);
print_r($blubbe r);

if (isset($blubber ))
print "I'm Blubber\n";
else
print "I'm Blubber no more\n";
</code>

noob would expect to see no print_r-output and the message should be

"I'm Blubber no more"

Actually, things are quite different:

1st: Object is printed per print_r

2nd: Message is "I'm Blubber"

Haven't I just killed the object...? Objects, when supplied as parameter,
are passed by reference in php5, aren't they?

OK, that obviously did not work (somehow... - remember, I'm a noob).

I reformulate the static kill_object-method:

public static kill_object(&$o bj)
{

if (!is_object($ob j))
return false;

$obj = null;

return true;

}

Note, that there comes the old (deprecated) reference-operator '&'
(that -
for objects - is no longer needed in php5, right?)

This time, however, the result is:

1st: Object is no longer printed (per print_r)

2nd: Message is "I'm Blubber no more"

Please assist!

cheers

juergen


First of all, static methods don't work on objects - they work on the
class.

But PHP will destroy an object sometime after the last reference to the
object is removed. If this is your ONLY reference to the object, at
some time, when the PHP garbage collector runs, the object will be deleted.

But this isn't necessarily the only reference to the object, and the
garbage collector won't necessarily run immediately.

It's not like C++ or SmallTalk, where the destructor can destroy an
object immediately.
In fact, when the reference to an object are set to NULL, the
object should be inaccessible from that reference. From the
programmers point of view, it should be the same object destroyed.

System may have need to keep the actual data, but that is the benefit of
GC, programmer does not need to manage memory allocation.

B
Jul 10 '08 #3
Hello Jerry, hello "Blueparty" ,

many thanks for your replies.

I know there is garbage collection. However, if you do have a relatively
large script, therein looping through a vast amount of data directories,
faced with the necessity to (re-)instantiate big (mostly aggregated)
objects inside various loop levels... - in situations like these it would -
for the sake of efficiency ?! - be nice to destroy what is no longer needed
(maybe I should change my programming style altogether; but that's a
different story).

Now to the problem itself: My premiss was, that

$object = null;

would effectively destroy the object - as it has been suggested in various
forums. What I erroneously attempted was to put this statement inside a
static method.

<code_version 1>

public static function kill_object($ob j)
{

if (!is_object($ob j))
return false;

$obj = null;

return true;

}

</code_version1>

I learned that calling the method

some_pseudo_sta tic_class::kill _object($someob ject);

by no means killed $someobject. At this point I was wondering why - since in
php5 objects are passed by reference... so passing it to the method and
setting it to null therein, was expected to do the job...

I then tried the deprecated way with the '&' operator:

<code_version 2>

public static function kill_object(&$o bj)
{

if (!is_object($ob j))
return false;

$obj = null;

return true;

}

</code_version2>

This one worked, so I became confused... I thought, with php5, '&' was no
longer needed...

The solution is explained in this article:

http://mjtsai.com/blog/2004/07/15/ph...ct-references/

It appears that my problem boils down to a misapprehension of what "passing
an object by reference" really means...

As stated in one of the comments following the article:

<cite>
holy cow, so in PHP5, $obj = $obj2
and $obj =& $obj2 are different things...
</cite>

....and so are

public static function kill_object($ob j)

public static function kill_object(&$o bj)

Cheers

Jürgen



Jul 11 '08 #4
Juergen-Bernhard Adler wrote:
Hello Jerry, hello "Blueparty" ,

many thanks for your replies.

I know there is garbage collection. However, if you do have a relatively
large script, therein looping through a vast amount of data directories,
faced with the necessity to (re-)instantiate big (mostly aggregated)
objects inside various loop levels... - in situations like these it would -
for the sake of efficiency ?! - be nice to destroy what is no longer needed
(maybe I should change my programming style altogether; but that's a
different story).

Now to the problem itself: My premiss was, that

$object = null;

would effectively destroy the object - as it has been suggested in various
forums. What I erroneously attempted was to put this statement inside a
static method.

<code_version 1>

public static function kill_object($ob j)
{

if (!is_object($ob j))
return false;

$obj = null;

return true;

}

</code_version1>

I learned that calling the method

some_pseudo_sta tic_class::kill _object($someob ject);

by no means killed $someobject. At this point I was wondering why - since in
php5 objects are passed by reference... so passing it to the method and
setting it to null therein, was expected to do the job...

I then tried the deprecated way with the '&' operator:

<code_version 2>

public static function kill_object(&$o bj)
{

if (!is_object($ob j))
return false;

$obj = null;

return true;

}

</code_version2>

This one worked, so I became confused... I thought, with php5, '&' was no
longer needed...

The solution is explained in this article:

http://mjtsai.com/blog/2004/07/15/ph...ct-references/

It appears that my problem boils down to a misapprehension of what "passing
an object by reference" really means...

As stated in one of the comments following the article:

<cite>
holy cow, so in PHP5, $obj = $obj2
and $obj =& $obj2 are different things...
</cite>

...and so are

public static function kill_object($ob j)

public static function kill_object(&$o bj)

Cheers

Jürgen
Yes, Jürgen, it can be confusing. In this case, the object is passed by
reference - but not the variable. So anything you do on the object will
affect the object in both places. But if you change the variable
($obj), it will not change the original variable.

A subtle, but important difference in some cases.

But you don't necessarily need to set the variable to null to get rid of
the object - setting the variable to any value (including that of a new
object of the same type) will do the same thing, assuming that is the
only reference to the object.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===

Jul 11 '08 #5

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

Similar topics

1
15262
by: Thomas Ilsche | last post by:
Hi, how do I "explicitly destroy" an Object in PHP5 to make sure the destructor is called an the object destroyed? unset is not an option because there are multiple variables containing the object handle. Waiting for the end of the script is aswell a bad option because the order in which the destructores are called does matter.
1
2265
by: Minkyu Kim | last post by:
Hi. Please check this simple test code. --------------------- class TestA: def __init__(self): print "init TestA" def __del__(self): print "del TestA" def SetEvent(self, event):
4
5531
by: Christopher Subich | last post by:
I'm building an application involving both twisted and Tkinter. Since twisted co-opts <widget>.mainloop() in its reactor.run(), and since it behaves very badly if the application quits without reactor.stop() running, I attach the following function to '<Destroy>' in the main window (root = Tk()): def stop_reactor_bind(x): reactor.stop() Then:
43
2663
by: Sarfraz Hooda | last post by:
Hi, I have created an array of Objects in a collection. I was wondering is there a way to destroy the array to free up the space in the memory ? or they are automatically destroyed and garbagge collected by .Net framework? Sarfraz
2
7060
by: Sarfraz Hooda | last post by:
Hi, I have created an array of Objects in a collection. I was wondering is there a way to destroy the array to free up the space in the memory ? or they are automatically destroyed and garbagge collected by .Net framework? Sarfraz
7
6307
by: Claire | last post by:
How do I make sure Ive disposed of my objects please? Ive added interface IDispose to a base class. Ive several sub classes inheriting from this base class. I store one or other of these subclasses in a variable and call dispose(), but the Dispose() method of the base class is called rather than in the sub class example class baseclass : IDisposable
8
1942
by: vvenk | last post by:
Hello: I just wrote my first ASP.Net application. It worked fine on my machine and when I put into production, the ASP.Net process reaches 50% quite fast and then the system does not work anymore until I kill that process. Obviously, this is not acceptable. Looking back, I do not destroy any objects in my form. Would that be the reasn why the application breaks down?
16
4538
by: LP | last post by:
Hello, I am trying to use .NET with Excel. I installed Office 2003 and selected ..NET programming suport option, so it installed all those PIA, as MS sugests. But I can not find a way to destroy Excel process, it still hangs in the taks manager' Processes as running. I am trying very simple code (see below), but Excel wont go away, I tried just about anything I could find on MSDN or by Google. Even calling WIN32 API to destroy Excel. But...
1
3190
by: Jonathan | last post by:
I referenced an mdb file to the main db using Tools -> Reference in the VBA Editor of the main mdb. I am able to open and use the forms in the referenced program, via a public sub that resides in a standard modual in the referenced program. My problem is catching an event when the user clicks a button on a form in the referenced program, and responding to the event in the main program.
0
8050
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7987
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8464
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...
0
6805
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
6000
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
5471
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();...
0
4015
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2464
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
1
1574
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.