c_pattle Posted February 6, 2011 Share Posted February 6, 2011 If I have defined an instance of a class in one script how do I then use this same instance and the same variable in other scripts? For example say I have this code in a file called "index.php" include("myclass.class.php"); $myclass = new myClass; $myclass->$variable = "hello world"; However if I then have this code in a file called "page1.php" when I try to access the same variable it is empty. include("myclass.class.php"); echo($myclass->$variable) How can I use the same instance of this class across all files keep the variable values the same? Thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/226865-php-classes/ Share on other sites More sharing options...
trq Posted February 6, 2011 Share Posted February 6, 2011 You would need to include page1.php wherever you need it. Quote Link to comment https://forums.phpfreaks.com/topic/226865-php-classes/#findComment-1170592 Share on other sites More sharing options...
c_pattle Posted February 6, 2011 Author Share Posted February 6, 2011 Thanks, but what about if page1.php is a completely seperate page with html and php. If I were to includes I'd end up with two html pages in 1. Quote Link to comment https://forums.phpfreaks.com/topic/226865-php-classes/#findComment-1170597 Share on other sites More sharing options...
c_pattle Posted February 6, 2011 Author Share Posted February 6, 2011 So basically what would I do if the two pages I am working with HAVE to be separate? Quote Link to comment https://forums.phpfreaks.com/topic/226865-php-classes/#findComment-1170601 Share on other sites More sharing options...
ignace Posted February 6, 2011 Share Posted February 6, 2011 Include the code: $myclass = new myClass; $myclass->$variable = "hello world"; In the file myClass.class.php Quote Link to comment https://forums.phpfreaks.com/topic/226865-php-classes/#findComment-1170608 Share on other sites More sharing options...
c_pattle Posted February 6, 2011 Author Share Posted February 6, 2011 Thanks but what about if I overwrite "$myclass->variable" several times on one page. How can I then make sure that I can access this variable on another page? Quote Link to comment https://forums.phpfreaks.com/topic/226865-php-classes/#findComment-1170637 Share on other sites More sharing options...
ignace Posted February 6, 2011 Share Posted February 6, 2011 If you want to persist information between requests you should use sessions. Quote Link to comment https://forums.phpfreaks.com/topic/226865-php-classes/#findComment-1170648 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.