Jump to content


Photo

Page Object Between Pages?


  • Please log in to reply
3 replies to this topic

#1 doni49

doni49
  • Members
  • PipPipPip
  • Advanced Member
  • 515 posts
  • LocationCentral MO

Posted 11 October 2006 - 01:50 PM

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.

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

$tstvar = new myClass;
?>

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?

$_SESSION['tstvar']=$tstvar;

TIA!
Don

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 11 October 2006 - 01:59 PM

Yes, storing it in a sessions is the 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;

session_start();
$obj = $_SESSION['myobj'];
$a = $obj->foo();

You could also use serialize / unserialize but essentually that is exactly what storing an object in a session does.

#3 doni49

doni49
  • Members
  • PipPipPip
  • Advanced Member
  • 515 posts
  • LocationCentral MO

Posted 11 October 2006 - 11:12 PM

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.
Don

#4 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 11 October 2006 - 11:28 PM

Not that Im aware of.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users