TomTees Posted September 30, 2010 Share Posted September 30, 2010 How do I access an object on different pages? I created object "A" on pageone.php and I need to access it on pagetwo.php. How do I do that? TomTees Quote Link to comment https://forums.phpfreaks.com/topic/214847-accessing-an-object-on-different-page/ Share on other sites More sharing options...
KevinM1 Posted September 30, 2010 Share Posted September 30, 2010 How do I access an object on different pages? I created object "A" on pageone.php and I need to access it on pagetwo.php. How do I do that? TomTees You'd have to pass it through sessions. Quote Link to comment https://forums.phpfreaks.com/topic/214847-accessing-an-object-on-different-page/#findComment-1117680 Share on other sites More sharing options...
TomTees Posted September 30, 2010 Author Share Posted September 30, 2010 How do I access an object on different pages? I created object "A" on pageone.php and I need to access it on pagetwo.php. How do I do that? TomTees You'd have to pass it through sessions. You can pass an entire Object through a Session?? Is this safe? :-\ Any "gotchas"? Could you give me a snippet showing me how to do this? Let's say I have... class Ticket{ private $price; private $destination; } $objTicket = new Ticket('$350', 'Miami'); on Booking.php and I need to have access to the object on RegistrationSummary.php How would I do that using a Session?? Thanks, TomTees Quote Link to comment https://forums.phpfreaks.com/topic/214847-accessing-an-object-on-different-page/#findComment-1117687 Share on other sites More sharing options...
PFMaBiSmAd Posted September 30, 2010 Share Posted September 30, 2010 $_SESSION['objTicket'] = new Ticket('$350', 'Miami'); Don't forget that your class definition needs to exist before the session_start() statement so that the object can be recreated from the session data file. Quote Link to comment https://forums.phpfreaks.com/topic/214847-accessing-an-object-on-different-page/#findComment-1117690 Share on other sites More sharing options...
TomTees Posted September 30, 2010 Author Share Posted September 30, 2010 $_SESSION['objTicket'] = new Ticket('$350', 'Miami'); Don't forget that your class definition needs to exist before the session_start() statement so that the object can be recreated from the session data file. Hmm... That is strange code?! Why don't you declare the class and instantiate it using normal syntax and then assign it to a session? <?php class Ticket{ $private price; $private destination; } $objTicket = new Ticket('$350', 'Miami'); ?>$_SESSION['objTicket'] TomTees Quote Link to comment https://forums.phpfreaks.com/topic/214847-accessing-an-object-on-different-page/#findComment-1117693 Share on other sites More sharing options...
kickstart Posted September 30, 2010 Share Posted September 30, 2010 Hi You could also serialise the object and store it in a file (useful if it is common between different sessions) All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/214847-accessing-an-object-on-different-page/#findComment-1117694 Share on other sites More sharing options...
ignace Posted October 1, 2010 Share Posted October 1, 2010 Why don't you declare the class and instantiate it using normal syntax and then assign it to a session? Because it's the same. class SomeClass { private $data = array('hello', 'world'); function __toString() { return serialize($this); } function __sleep() { return array('data'); } } $c = new SomeClass(); echo serialize($c), ' ', $c, "<br>\n"; $s = serialize($c); $d = strval($c); var_dump(unserialize($s)); var_dump(unserialize($d)); Quote Link to comment https://forums.phpfreaks.com/topic/214847-accessing-an-object-on-different-page/#findComment-1117844 Share on other sites More sharing options...
TomTees Posted October 1, 2010 Author Share Posted October 1, 2010 Why don't you declare the class and instantiate it using normal syntax and then assign it to a session? Because it's the same. class SomeClass { private $data = array('hello', 'world'); function __toString() { return serialize($this); } function __sleep() { return array('data'); }} $c = new SomeClass();echo serialize($c), ' ', $c, "<br>\n";$s = serialize($c);$d = strval($c);var_dump(unserialize($s));var_dump(unserialize($d)); You guys are losing me here... I was under the impression that if I store an Object in a Session then that is all I need to do (and then I do NOT have to Serialize the Object as well). Is that correct? I thought that is what the PHP manual said. Also, what is the fascination with Serialization by some people? From what I have read, Serialization is so "old school" and just using Sessions is better. TomTees Quote Link to comment https://forums.phpfreaks.com/topic/214847-accessing-an-object-on-different-page/#findComment-1117980 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.