Click here to get back home

destroy referenced objects

 HomeNewsGroups | Search | About
 comp.lang.php    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
destroy referenced objects Juergen-Bernhard Adler 07-09-2008
Get Chitika Premium
Posted by Juergen-Bernhard Adler on July 9, 2008, 7:56 pm
Please log in for more thread options
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


Posted by Jerry Stuckle on July 9, 2008, 11:32 pm
Please log in for more thread options
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.
jstucklex@attglobal.net
==================


Posted by Blueparty on July 10, 2008, 2:04 pm
Please log in for more thread options
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

Posted by Juergen-Bernhard Adler on July 10, 2008, 8:17 pm
Please log in for more thread options
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/php-5-object-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












Posted by Jerry Stuckle on July 10, 2008, 9:00 pm
Please log in for more thread options
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/php-5-object-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.
jstucklex@attglobal.net
==================


Similar ThreadsPosted
explicitly destroy Objects in PHP5 April 18, 2005, 8:01 am
Cookies being referenced in a second domain January 25, 2005, 3:32 pm
Problems including a file referenced from SERVER['DOCUMENT_ROOT'] December 28, 2006, 11:30 am
can't destroy sessions October 25, 2004, 10:42 am
sessions gc and destroy December 18, 2004, 6:14 am
What does session_destroy() actually destroy? January 18, 2005, 4:16 pm
Destroy $_POST vars after use - is it possible? June 10, 2005, 1:59 pm
Session close, destroy and regenerate January 24, 2005, 7:20 pm
Session destroy when closing browser April 17, 2008, 6:26 am
how to declare a cookie that will destroy after browser is closed January 31, 2007, 1:42 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap