keyongtech


  keyongtech > php

 #1  
07-10-08, 12:56 AM
Juergen-Bernhard Adler
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
 #2  
07-10-08, 04:32 AM
Jerry Stuckle
Juergen-Bernhard Adler wrote:
[..]
>
> 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.
 #3  
07-10-08, 07:04 PM
Blueparty
Jerry Stuckle wrote:
> Juergen-Bernhard Adler wrote:
>
> 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
 #4  
07-11-08, 01:17 AM
Juergen-Bernhard Adler
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
 #5  
07-11-08, 02:00 AM
Jerry Stuckle
Juergen-Bernhard Adler wrote:
[..]
> ...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.
Similar Threads
Thread Thread Starter
handling of regexp objects that aren't referenced by variables,arrays, tables or objects

Hi, first of all I have to say I'm relatively unexperienced with Ruby and also new to regular expressions. This causes me some problems: I'm parsing text files and am...

ThomasW
How can I destroy objects like an ArrayList?

Using Powershell v1 atm.. I'm working on a large complex script and need to be able to destro objects, specifically ArrayLists. How can I do so This is how I create...

ioioio322
Destroy Objects How ?

hi, To destroy objects in vb6 we used to write Set MyObj = Nothing But how do i destroy objects to clean up memory in vb.net 2005 MyObj.Dispose

Abhishek
Disposing. My objects won't destroy

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

Claire
Problems when destroy object which is referenced member objects

Hi. Please check this simple test code. --------------------- class TestA: def __init__(self): print "init TestA" def __del__(self): print "del TestA" def SetEvent(self,...

Minkyu Kim

Privacy Policy | All times are GMT. The time now is 03:38 PM.

Merging Information Logo
[Deutschland] [España] [France] [Italia] [Nederland] [Polska] [United Kingdom]