Saturday, 24 August 2013

How would I manage the concept of a User object in PHP?

How would I manage the concept of a User object in PHP?

Okay, I'm still very new to OOP overall, but I've created a few workable
classes now and I'm slowly grasping the various concepts of OOP in PHP. On
a project that I'm working on currently, a user can register,
login/logout, and upload/delete/comment/rate images.
Currently, when a user logs in, I put most of their information (that I
have retrieved from a database) inside the $_SESSION superglobal. This
includes information such as their first name, username, user_id, etc.
Complementing this, I have a page called user.functions.php which has a
number of miscellaneous functions that are related to the concept of a
'user', such as permissions checking, requesting additional user data,
checking a successful login, registering a user, etc.
My question is how would I manage this as a User class? If I understand
correctly, anything called on a page exists on that page only, so wouldn't
I have to declare a
new User();
on each page where I need to perform a task relating to that user? How is
that in anyway efficient? Isn't it just simpler to call a function like:
changeAddress();
instead of:
$user = new User();
$user->changeAddress();
What about critical user data such as the user_id, which I currently store
in a session? Would I migrate that data inside a class too and retrieve it
via a getter method?
Basically, I fail to see how the concept of a user object can improve
overall code quality, if a user, which is an inherently cross-page
concept, is tied to an object, which exists solely within the confines of
a single document?
What am I missing here?

No comments:

Post a Comment