earachefl Posted February 28, 2011 Share Posted February 28, 2011 Just get started with OO in PHP. If I create an object while on one page and then call another page, can I still access that first object, or is it destroyed when the second page is called? For example, if I have on page1.php: $obj = new $MyObject(); and then call page2.php, is it possible to still access $obj while on that page? Quote Link to comment https://forums.phpfreaks.com/topic/229168-oo-question/ Share on other sites More sharing options...
AbraCadaver Posted February 28, 2011 Share Posted February 28, 2011 No. You would need sessions to carry vars across pages. Or if it is long lasting store it in a database. Quote Link to comment https://forums.phpfreaks.com/topic/229168-oo-question/#findComment-1180933 Share on other sites More sharing options...
PFMaBiSmAd Posted February 28, 2011 Share Posted February 28, 2011 You would need to use a $_SESSION variable - Page where object is created - <?php // the class definition must exist before the session_start() so that the object can be recreated session_start(); $_SESSION['obj'] = new $MyObject(); Page that references the object - <?php // the class definition must exist before the session_start() so that the object can be recreated session_start(); echo $_SESSION['obj']->some_property; echo $_SESSION['obj']->some_method(); Quote Link to comment https://forums.phpfreaks.com/topic/229168-oo-question/#findComment-1180934 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.