Jump to content

[SOLVED] OOP - Session and Class Objects


premiso

Recommended Posts

Hey,

 

Just curious on storing Objects in Session with PHP. I know .NET does this regularly and was wondering if anyone had any reason why this would be avoided.

 

Basically I am going to have a Person Class that extends Address. A person can have 1 or more addresses. I want to store this person class in an object in session when they are modifying their data (UserCP). So instead of having to re-query each time, I just store that in session and make an isDirty variable so when they are done, it checks that variable. If anything has been changed it updates the database.

 

I do not really see anything wrong with this, but given that PHP's OOP is still getting the tweaks worked out I figured I would see if anyone had any thoughts.

 

Thanks!

Link to comment
Share on other sites

This has worked forever (only requirement is the class definition must exist before session_start() so that the data can be restored) -

<?php
require 'your_class.php';
session_start();
if(!isset($_SESSION['some_object_name'])){
// create instance
$_SESSION['some_object_name'] = new your_class();
echo 'object created<br />';
} else {
echo 'object exists<br />';
}

$_SESSION['some_object_name']->class_function();
echo $_SESSION['some_object_name']->class_variable;
?>

Link to comment
Share on other sites

This has worked forever (only requirement is the class definition must exist before session_start()...

 

That must be the kicker that I never got to work.

 

Thanks for that PFMaBiSmAd.

 

Yea, I have seen the autoload, I have not looked into it. But I will and see if that works. Thanks.

Link to comment
Share on other sites

This has worked forever (only requirement is the class definition must exist before session_start() so that the data can be restored)

 

As a follow-up question, what about us folks who start sessions automatically in the ini file ?

 

For my Oracle Object, I get "Incomplete_Object" because of it... It doesn't seem to be causing my application any harm...  Is it "safe" to continue using an incomplete object ? What are the ramifications ?

 

Thanks,

 

Scot L. Diddle, Richmond VA

 

Link to comment
Share on other sites

This has worked forever (only requirement is the class definition must exist before session_start() so that the data can be restored)

 

As a follow-up question, what about us folks who start sessions automatically in the ini file ?

 

For my Oracle Object, I get "Incomplete_Object" because of it... It doesn't seem to be causing my application any harm...  Is it "safe" to continue using an incomplete object ? What are the ramifications ?

 

Thanks,

 

Scot L. Diddle, Richmond VA

 

 

I would use the autoloader as pointed out. As far as the incomplete object, I do not think it is kosher. If that does not answer your question, open up a new topic please.

 

Topic Solved.

Link to comment
Share on other sites

If your session is automatically started in php.ini, I believe you are out of luck using the direct ($_SEESION[...] = new class();) method shown. You would need to fall back to use an indirect method of storing and retrieving the object variables in the session.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.