Jump to content

Page Object Between Pages?


doni49

Recommended Posts

I DID do a search but a bunch of other junk came up.  I guess I'm not using a good search parameter, but I'm drawing a blank one what WOULD be a good search.  I'm trying to find out how to pass an object (a class that i created) from one page to another.

Do I set a session variable equal the variable that the object is assigned to?  Will the whole object be available in the next page?  Is that the best way to do it or is there a better way (more efficient use of memory)?

I've got a small example class below of course my REAL class will be much larger and more complicated which is why I'm asking about memory usage.

[code]
<?php
class myClass{
    var $test1=1;
    var $test2=2;
    var $test3;
    function myClass(){
        $this->test3 = $this->test1 + $this->test2;
    }
}

$tstvar = new myClass;
?>
[/code]

I'm thinking that the following would make the entire object available on the next page, but I'm not sure it's the best use of memory.  Is there a better way of doing it?

[code]
$_SESSION['tstvar']=$tstvar;
[/code]

TIA!
Link to comment
Share on other sites

Yes, storing it in a sessions is [i]the[/i] way to do it. Just remember on page 2 you'll need to save it back into a local var before trying to access it. eg;

[code=php:0]
session_start();
$obj = $_SESSION['myobj'];
$a = $obj->foo();
[/code]

You could also use serialize / unserialize but essentually that is exactly what storing an object in a session does.
Link to comment
Share on other sites

Thanks.  I kinda figured that.  But I wasn't sure if there was a better way to do it.

I have one other Q about this.  Is there a limit to the amount of data that can be passed via session?  Either the session overall or the single session variable that contains this particular object.
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.