473,395 Members | 1,885 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,395 software developers and data experts.

destroy referenced objects

Hello,

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

public static kill_object($obj)
{

if (!is_object($obj))
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($blubber))
exit(0);

print_r($blubber);

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(&$obj)
{

if (!is_object($obj))
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 2011
Juergen-Bernhard Adler wrote:
Hello,

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

public static kill_object($obj)
{

if (!is_object($obj))
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($blubber))
exit(0);

print_r($blubber);

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(&$obj)
{

if (!is_object($obj))
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*******@attglobal.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($obj)
{

if (!is_object($obj))
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($blubber))
exit(0);
print_r($blubber);

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(&$obj)
{

if (!is_object($obj))
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_version1>

public static function kill_object($obj)
{

if (!is_object($obj))
return false;

$obj = null;

return true;

}

</code_version1>

I learned that calling the method

some_pseudo_static_class::kill_object($someobject) ;

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_version2>

public static function kill_object(&$obj)
{

if (!is_object($obj))
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($obj)

public static function kill_object(&$obj)

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_version1>

public static function kill_object($obj)
{

if (!is_object($obj))
return false;

$obj = null;

return true;

}

</code_version1>

I learned that calling the method

some_pseudo_static_class::kill_object($someobject) ;

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_version2>

public static function kill_object(&$obj)
{

if (!is_object($obj))
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($obj)

public static function kill_object(&$obj)

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*******@attglobal.net
==================

Jul 11 '08 #5

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

Similar topics

1
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...
1
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
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...
43
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...
2
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...
7
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...
8
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...
16
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...

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.