paraleadogg Posted March 31, 2009 Share Posted March 31, 2009 Hi, I have a quick question. I am learning OO PHP and wondered if someone could help. I have a class called user This class contains parameters (firstname, username etc) and methods (getDetails()) If i create an instance of a user in my index page like this: require('user_class.php'); $user = new user(); $user->getDetails; echo $user->firstname; This of course works fine. However if i then go to a new page (example.php) and try and access the $user object i cant. I have to instantiate it again $user = new user(); And then get details AGAIN $user getDetails(); My question is - how can create the object once and have it universally available throughout my site?? it seems stupid to have to create a new instance on every single page?? Link to comment https://forums.phpfreaks.com/topic/151859-universal-objects/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 31, 2009 Share Posted March 31, 2009 http://us.php.net/manual/en/book.session.php As long has your class definition exists before the session_start() statement, you can create or use a session variable for the instance of your class - http://www.phpfreaks.com/forums/index.php/topic,241722.0.html Link to comment https://forums.phpfreaks.com/topic/151859-universal-objects/#findComment-797443 Share on other sites More sharing options...
paraleadogg Posted March 31, 2009 Author Share Posted March 31, 2009 http://us.php.net/manual/en/book.session.php As long has your class definition exists before the session_start() statement, you can create or use a session variable for the instance of your class - http://www.phpfreaks.com/forums/index.php/topic,241722.0.html Hmmm ok - I didnt realise you could store an object in a session But that then begs the question - what is the point of the object then?? Icould just set some session variables directly from the database result set no? Link to comment https://forums.phpfreaks.com/topic/151859-universal-objects/#findComment-797451 Share on other sites More sharing options...
9three Posted March 31, 2009 Share Posted March 31, 2009 Object Oriented Programming is sorta like a generic skeleton for your application. The benefit of storing information in a session is that you don't need a database, thus, increasing the speed of your application. In addition, you can access the information from any page, as long as you start your application with session_start(). Link to comment https://forums.phpfreaks.com/topic/151859-universal-objects/#findComment-797478 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.