how to better retrieve user object

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
jonwondering
Forum Commoner
Posts: 39
Joined: Mon Mar 13, 2006 6:26 pm

how to better retrieve user object

Post by jonwondering »

This question must be asked a lot, but I still couldn't find anything good. I have a lot of php pages that need to use the user object. What I am wondering is what would be a better way to handle this:

1. Make a global user object once logged in, and re-retrieve it only when it is updated, or
2. Create a new local user object for every page if logged in, and go from there.

Obviously the first one will make less queries to the database, but I am still wondering how others handle this. Maybe there's another option. Any ideas?

Thanks.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

both are fine - I usually cache it in $_SESSION, but you can retrieve it on page load as well, your database won't die under the extra load.

Alternatively, you could cache it on the filesystem and include it on every page, re-writing the cache's copy on an update.
jonwondering
Forum Commoner
Posts: 39
Joined: Mon Mar 13, 2006 6:26 pm

Post by jonwondering »

so selecting a single row from mysql every time a page loads should not mess up mysql too much, right?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

no - IMO people usually spend far too much time saving a few MySQL queries here and there. They're pretty cheap these days!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Yeah, I think saving the user object in the session (whether file or DB based) is probably the easiest way. It is probably on of the few example of a where a Singleton makes sense in PHP.
(#10850)
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

arborint wrote:Yeah, I think saving the user object in the session (whether file or DB based) is probably the easiest way. It is probably on of the few example of a where a Singleton makes sense in PHP.
I have thought about that too (storing object in session) and it would make perfect sense. But can someone give us a simple example on how this will actually work. Simple usage and update scenario.
Cause I think there will be resources to be recreated on unserialize (when you get the object from SESSION etc). I guess __wakeup will be used etc.
Is it as complex as I think it is?
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

As I've seen in the two widely used applications
1) is creating a sessions_table in the database and using it for session_storage (Application: Dotproject)
2)Using Singletonclass you can acheive a constant connection to the database once the user is logged in (Application: Wordpress).

Point 2 has to be applied carefully with lot of considerations. For that you can check the following
Singleton Considerations
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

dude81 wrote:As I've seen in the two widely used applications
1) is creating a sessions_table in the database and using it for session_storage (Application: Dotproject)
2)Using Singletonclass you can acheive a constant connection to the database once the user is logged in (Application: Wordpress).

Point 2 has to be applied carefully with lot of considerations. For that you can check the following
Singleton Considerations
Question is not where session is stored, but rather what you store in there and how you make benefit from it. Are you making ..?

Code: Select all

$user = new User('userid');
$_SESSION['user']  = $user;
And then ...get user object from there, alter on update only, so you don't instantiate connections ...and select all data about user per each request.
Post Reply