sinista Posted December 2, 2009 Share Posted December 2, 2009 hey, if I create an object on one page how do I send it to another page? thanks Quote Link to comment https://forums.phpfreaks.com/topic/183708-sending-objects-from-page-to-page/ Share on other sites More sharing options...
JonnoTheDev Posted December 2, 2009 Share Posted December 2, 2009 Store it in a session <?php session_start(); $x = new foobar(); $_SESSION['object'] = $x; ?> Generally used for the likes of shopping carts Quote Link to comment https://forums.phpfreaks.com/topic/183708-sending-objects-from-page-to-page/#findComment-969605 Share on other sites More sharing options...
sinista Posted December 2, 2009 Author Share Posted December 2, 2009 k, cheers:) am I doing this right? if I set the object as a session variable I to serialise it into a string first then unserialize in on the next page? Quote Link to comment https://forums.phpfreaks.com/topic/183708-sending-objects-from-page-to-page/#findComment-969611 Share on other sites More sharing options...
JonnoTheDev Posted December 2, 2009 Share Posted December 2, 2009 You are correct, however I think this is done automatically when the session variable is registered. Not had to use the serialize() function when registering objects Quote Link to comment https://forums.phpfreaks.com/topic/183708-sending-objects-from-page-to-page/#findComment-969618 Share on other sites More sharing options...
sinista Posted December 2, 2009 Author Share Posted December 2, 2009 I don't know what I have done, but if I try and echo the sessions var on the next page it is empty, but I can see the variables have been created by using print_r() I would seem that its easier (for me any way) to post a reference to the object around, thanks for your help though. Quote Link to comment https://forums.phpfreaks.com/topic/183708-sending-objects-from-page-to-page/#findComment-969627 Share on other sites More sharing options...
JonnoTheDev Posted December 2, 2009 Share Posted December 2, 2009 Remember you must use session_start() on all pages that require access to session data. Are you familiar with sessions? Quote Link to comment https://forums.phpfreaks.com/topic/183708-sending-objects-from-page-to-page/#findComment-969630 Share on other sites More sharing options...
sinista Posted December 2, 2009 Author Share Posted December 2, 2009 yeah the sessions started, I'm kind of making progress though if I echo the session var on the next page I get a 'could not convert to string error' if I wrap it in serialise() i can see my object Quote Link to comment https://forums.phpfreaks.com/topic/183708-sending-objects-from-page-to-page/#findComment-969636 Share on other sites More sharing options...
JonnoTheDev Posted December 2, 2009 Share Posted December 2, 2009 echo the session var on the next page You dont echo objects <?php // page1.php session_start(); $x = new foobar(); $x->name = "John Doe"; $_SESSION['object'] = $x; header("Location:page2.php"); exit(); ?> <?php // page2.php session_start(); echo $_SESSION['object']->name; ?> Quote Link to comment https://forums.phpfreaks.com/topic/183708-sending-objects-from-page-to-page/#findComment-969638 Share on other sites More sharing options...
sinista Posted December 2, 2009 Author Share Posted December 2, 2009 thanks neil you fixed it, one other thing it doesn't work unless I include the original class (on the second page), before you start the session? I was under the impression that you should start the session first if your using them? Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/183708-sending-objects-from-page-to-page/#findComment-969691 Share on other sites More sharing options...
PFMaBiSmAd Posted December 2, 2009 Share Posted December 2, 2009 The class definition needs to exist before the session_start() so that the object can be recreated correctly. I was under the impression that you should start the session first if your using them?The only requirement is that the session_start() must come before any output is sent to the browser. Including/defining any classes/functions/settings that are used on the page can be thought of as an initialization/setup step for the page and generally should be done before the page actually does something (like a session_start() statement.) Quote Link to comment https://forums.phpfreaks.com/topic/183708-sending-objects-from-page-to-page/#findComment-969693 Share on other sites More sharing options...
sinista Posted December 2, 2009 Author Share Posted December 2, 2009 I got another question about objects when do they get disposed and does everything created through them die with them? I have a method that set a session variable then returns a value, if I change the page I cant use the variable, so where has it gone? I've tried this with a cookie to and they both disappear. Quote Link to comment https://forums.phpfreaks.com/topic/183708-sending-objects-from-page-to-page/#findComment-969711 Share on other sites More sharing options...
PFMaBiSmAd Posted December 2, 2009 Share Posted December 2, 2009 Both a $_SESSION variable and a class variable (with the instance of the class in a session variable) would be available when the session is resumed, assuming your session is actually being started and resumed. It would take seeing the code that is not working to be able to determine what it might be doing that is causing the symptom and are you developing and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON in your master php.ini so that php would help you by displaying all the errors it detects? Quote Link to comment https://forums.phpfreaks.com/topic/183708-sending-objects-from-page-to-page/#findComment-969726 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.