Page 1 of 1
keeping objects open.
Posted: Tue Feb 11, 2003 9:57 am
by Heavy
I wonder what people do to keep objects open between requests.
My guess would be sessions. I have successfully accomplished that by doing something like this:
first, an include file:
Code: Select all
<?php
//inc.php
class MyObj{
var $value;
}
?>
Initialize and set:
Code: Select all
<?php
include 'inc.php';
$_SESSION['object'] = new MyObj;
?>
echo to browser:
Code: Select all
<?php
include 'inc.php';
echo $_SESSION['object']->value ;
?>
This works (yet, I did not test this one, just typed it in).
I realize sessions could be managed in a way that session data is stored in a database.
Is this given example the way to go?
Posted: Tue Feb 11, 2003 10:30 am
by kcomer
I have always had to serialize and unserialize objects when passing them between sessions. I believe you still have to do this with php, but I may be wrong. You don't need to serialize sessions though if you use session_register and not $_SESSION to set the session vriables. session_register auto serializes that object for you. Let me know what works for you. I use objects with sessions alot and anything to help me understand it better is great.
keith
Posted: Tue Feb 11, 2003 3:09 pm
by Heavy
If the object is serialized before end of script, do you still have to include the class definition in another file that uses but does not instantiate the object?
Eh, I could go home and try... The hour is 22:00, so I should really get myself home from work...
Posted: Tue Feb 11, 2003 3:33 pm
by kcomer
If the object is serialized before end of script, do you still have to include the class definition in another file that uses but does not instantiate the object?
Do you mean use without iinstantiating an object like this?
Or just not using the object and keeping it in the session variable? I don;t think you would have to include the class defenition if you just passed the serialized version of the object around.
Keith
Posted: Tue Feb 11, 2003 4:19 pm
by hedge
You don't need to serialize, php serialized the session data already. Here is what I do
Code: Select all
class cSession {
var $userName;
var $queryStr;
}
session_start();
if (!isset($_SESSIONї'session'])) {
$_SESSIONї'session'] = new cSession;
$_SESSIONї'session']->userName = '';
$_SESSIONї'session']->queryStr = '';
if (isset($_COOKIEї'user'])) $_SESSIONї'session']->userName = $_COOKIEї'user'];
}