Jump to content

Archived

This topic is now archived and is closed to further replies.

doni49

Page Object Between Pages?

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!

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.