Hello,
This is a question relating to the new PHP5 Object Destructor
functionality. Consider the following:
<?php
class DB
{
function Query() { ... }
}
class Session
{
function SetVar() {
//sets a variable to be saved in the session
}
function __destruct() {
//Save session variables in database using global $oDB object.
global $oDB;
$oDB->Query(...);
}
}
$oDB = new DB();
$oSession = new Session();
$oSession->SetVar('foo', 'New Value');
//End of script
?>
Based on the above example, the $oSession->__destruct() method relies on
the $oDB object still being usable. How can this be structured to ensure
that the DB object does not get released first?
Thanks,
Jason
Jason Garber
President & Chief Technology Officer
IonZoft, Inc.
814.742.8030 :: jason@ionzoft.com :: http://IonZoft.com
Jason Garber wrote:
Based on the above example, the $oSession->__destruct() method relies on
the $oDB object still being usable. How can this be structured to
ensure that the DB object does not get released first?
Adding something like $oSession->db = $oDB; should work in my opinion.
Didn't test it and I have no real experience with destructors.
- Chris
Jason Garber wrote:
Based on the above example, the $oSession->__destruct() method relies on
the $oDB object still being usable. How can this be structured to
ensure that the DB object does not get released first?
It can't.
Adding something like $oSession->db = $oDB; should work in my opinion.
Didn't test it and I have no real experience with destructors.
That's not guaranteed to work. PHP 5 does not guarantee that object
destructors will be called in any particular order or that the order
will remain constant from one invocation to another.
Before asking for changes to this behavior, please check the mailing
list archives for a long discussion on the topic. :)
-adam
--
adam@trachtenberg.com
author of o'reilly's php cookbook
avoid the holiday rush, buy your copy today!
Adam Maccabee Trachtenberg wrote:
That's not guaranteed to work. PHP 5 does not guarantee that object
destructors will be called in any particular order or that the order
will remain constant from one invocation to another.
Ok, I was under the impression that PHP won't call any destructors of
objects still in use but from your comment I guess it does two passes:
First collect a list of objects to destruct (which will be everything at
the end of the script) and then call all the destructors (in random order).
Ignore my first comment, it was posted without any insight in the
internals of the GC mechanism :-)
- Chris
Christian Schneider wrote:
Adam Maccabee Trachtenberg wrote:
That's not guaranteed to work. PHP 5 does not guarantee that object
destructors will be called in any particular order or that the order
will remain constant from one invocation to another.Ok, I was under the impression that PHP won't call any destructors of
objects still in use but from your comment I guess it does two passes:
First collect a list of objects to destruct (which will be everything at
the end of the script) and then call all the destructors (in random order).
unless you manually unset the objects in the order in which you want
them to be destructed.
regards,
Lukas Smith
unless you manually unset the objects in the order in which you want
them to be destructed.
Right. That'll also work. :)
-adam
--
adam@trachtenberg.com
author of o'reilly's php cookbook
avoid the holiday rush, buy your copy today!
Right. That'll also work. :)
I suppose you could push object references on a stack and register a
shutdown function which pops / unset()s if you really wanted a order to
it.. (that's an untested theory though)
John
--
-=~=--=~=--=~=--=~=--=~=--=~=--=~=--=~=--=~=--=~=--=~=--=~=-
John Coggeshall http://www.coggeshall.org/
The PHP Developer's Handbook http://www.php-handbook.com/
-=~=--=~=--=~=--=~=--=~=--=~=--=~=--=~=--=~=--=~=--=~=--=~=
John Coggeshall wrote:
Right. That'll also work. :)
I suppose you could push object references on a stack and register a
shutdown function which pops / unset()s if you really wanted a order to
it.. (that's an untested theory though)
Which is what PEAR does to "emulate" destructors. But its doesnt really
give you much control over the order of the stack. Its just the order in
which you have created your instances. Anyways this is more and more
turning into php-general@ material :-)
regards,
Lukas Smith