keeping objects open.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

keeping objects open.

Post 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?
kcomer
Forum Contributor
Posts: 108
Joined: Tue Aug 27, 2002 8:50 am

Post 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
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Post 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...
kcomer
Forum Contributor
Posts: 108
Joined: Tue Aug 27, 2002 8:50 am

Post 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?

Code: Select all

objectName::getItems();
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
hedge
Forum Contributor
Posts: 234
Joined: Fri Aug 30, 2002 10:19 am
Location: Calgary, AB, Canada

Post by hedge »

You don't need to serialize, php serialized the session data already. Here is what I do

Code: Select all

class cSession &#123;
   var $userName;
   var $queryStr;
 &#125;

 session_start();

 if (!isset($_SESSION&#1111;'session'])) &#123;
   $_SESSION&#1111;'session'] = new cSession;
   $_SESSION&#1111;'session']->userName = '';
   $_SESSION&#1111;'session']->queryStr = '';
   if (isset($_COOKIE&#1111;'user'])) $_SESSION&#1111;'session']->userName = $_COOKIE&#1111;'user'];
 &#125;
Post Reply