Jump to content

when to OOP...


jonniejoejonson

Recommended Posts

By "store this object in a session" I assume you mean create the object as a session variable -

 

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

$_SESSION['user_object_name']->class_function();  // example call to class function
echo $_SESSION['user_object_name']->class_variable; // example access of class variable
?>

Link to comment
Share on other sites

Depending on how I use the class... I may just store a value in the session instead from which I can initiate the class again on the next page and recreate the object.

 

Say my class dealt with file manipulation.. perhaps I just initiate the class with a filename and it gives me all I need from it... so i would just store the filename in the session to call the class again when I need it...

Link to comment
Share on other sites

Don't forget that when you use custom objects in sessions you need to load them before session_start on each request otherwise it won't be able to re-create the custom object as it can't find the class definition

 

If you use __autoload this is not an issue.  storing an object in a session means the object gets auto-magically serialized and un-serialized - you can also control that using the __sleep and __wake methods within the class itself.

Link to comment
Share on other sites

Don't forget that when you use custom objects in sessions you need to load them before session_start on each request otherwise it won't be able to re-create the custom object as it can't find the class definition

 

If you use __autoload this is not an issue.  storing an object in a session means the object gets auto-magically serialized and un-serialized - you can also control that using the __sleep and __wake methods within the class itself.

 

That is if you load it (declare it) before session_start() in both cases. Like I said here:

 

Don't forget that when you use custom objects in sessions you need to load them before session_start on each request otherwise it won't be able to re-create the custom object as it can't find the class definition

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.