sms.sanal Posted July 27, 2009 Share Posted July 27, 2009 how Can we store a class variable in session? Quote Link to comment https://forums.phpfreaks.com/topic/167575-storing-class-variable-in-session/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 27, 2009 Share Posted July 27, 2009 As long as the class definition exists before the session_start() statement you can create/reuse an instance of a class in a session variable - <?php require 'your_class.php'; session_start(); if(!isset($_SESSION['some_object_name'])){ // create instance $_SESSION['some_object_name'] = new your_class(); echo 'object created<br />'; } else { echo 'object exists<br />'; } $_SESSION['some_object_name']->class_function(); echo $_SESSION['some_object_name']->class_variable; ?> Simple class used by the above demo code - file your_class.php - <?php class your_class { public $class_variable = "class variable<br />"; public function class_function(){ echo "class function<br />"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/167575-storing-class-variable-in-session/#findComment-883726 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.