This question might sound strange, but I have a simply tiny session-based login system (PHP+MySQL) for one of my projects, and I was wondering if it would make any sense to store a user in an object. I could imagine such situations, where doing this could be useful, but… I don’t really know. Does it make difference in case of let’s say a microblogging system?
I think there could be a User class defined, with properties loaded from the MySQL tables, and it could have methods/functions like getUserName() or logOut() or addPost().
The top reason I asked myself this question, is that I don’t have a single clue about how to pass an object to the session, plus all this object-oriented stuff is a little bit new for me as well. I’m not entirely sure about when to think with OOP and when to not.
If you could give me some advices about the topic, or anything useful, it would be extremely helpful. Thanks! 🙂
Since there is no significant overhead for objects in PHP 5.3/5.4, I would recommend you to write code in object oriented style for everything, except simple experiments in codepad.
As for the authentication system, i wold split it up in two parts:
Userinstance to DBHere is a simple API example that you might try to use as basis for your code:
You have to understand that the aim of OOP is the maintainability and readability of the code. I assume that you can figure out what goes on in that code snipped without seeing the exact implementation of each method.