Recently I'm working on a User class. I need some help with this class to get it on track.
For example, I want a user to get logged in, how would I do this? Do I need to pass the HTML form to the login() function? Or do I need to pass all variables (like email and password) as variables, or?
Also, how to correctly interact with the Database class I've written? Should I do something like this?
$m_pConn = new Database();
Or am I thinking in the wrong way? Oh well, someday I will fully understand the OOP way of thinking. Thanks in advance for your replies.
This is what I have thus far:
// Do login here.
return (bool) $m_bLoggedIn;
you just need to pass in the username and password to the login then you do your query to check if its a valid login or not. the login function should return a true or false. The db class can be created like that, that is fine. You will have to store this class in a session variable to keep from reloading it every page as well.
That above bit goes in the bit that says, if post etc.
Then on the login bit you need to make sessions etc. I will post my one (Please note this wont work properly as it integrates methods etc that my framework has so you will need to change it a lot but you will be able to see what i mean):
Please note that its not perfect as i did code it at like 3am.
function login($userData, $redirect = '')
if(!empty($userData['username']) and !empty($userData['password']))
$password = $this->core->app->generateHash($userData['password'], $userData['username']);
$data = array('userId','username','password');
$sql = "SELECT userId
WHERE username = '" . $userData['username'] . "'
AND password = '$password'
personally I just store the entire user class. Mine is pretty small with only about 8-10 vars and just a couple functions.
A second reason why I save the entire class is so that I don't have to keep doing the same sql queries every time I load the page to get the users name ect. I only run them once when the user logs in. and can then easily access them any time I need.
At the moment I'm wondering if it's better do do something like this:
Create a Login class and a User class. First the user logs in via the Login class. If succesfully logins, the Login class creates a User class with the id of the user f.e. like $pUser = new User(1);. Then the Login class puts the User class in a session.
Shouldn't this be more logically (while thinking OOP)? Or am I just lost again? :-p
That would work, however on one hand does your login class really have much to it? Personally I only have 2 functions and no vars that go with my login. So just adding those 2 to my user class is not much to store. But if you have quiet a few items that are only used in your login then it might make sense to split it up. I don't think it will make a difference one way or the other its really just how you want to do it.