Jump to content

Beginning OO PHP..Create User Class query


tmfl

Recommended Posts

Hi,

 

I've always used PHP code in my websites in a procedural manner as I come from a html/css background. Now that I want to do projects with more complexity I'm thinking I need to approach development in a more Object Oriented fashion.

 

My question is more a theoretical one. I'm creating a User class and I'm not sure on what is the best practise for putting my functions etc. and where to call them.

 

My project will have 2 different users (Admin & End User) so is it correct to say that the User class should encapsulate all the functions of both of these users ? By that I mean create/delete/modify user, login & change password/forgotten password functionality rather than having a seperate Login class

 

Should I only call the database functions within these methods of the user class as opposed to calling them on the php page where the form data is posted ?

 

For example, if a user logins in and when the form data is posted I currently open a connection, validate data, run the query.

In an OO approach would I have just one User class method thats relevant to a particular page functionality ?

Say, in pseudo code, for handling a login request

 

loginRequest.php

include User class

try {
create new user User();
User::login();
}
catch Exception()


 

and should my User class look something like this

 

class.User.php

include DBase class

class User {
var name,
var email,
var password,

function validateInput (email, password) {
     if (!valid input)
         throw Exception
    else
       return true
}

function login (email, password){
    try {
       database connection
       validateInput();
       sql to see if user exists and login
       
   }
   catch Exception
}

}

 

Apologies for this being a bit long winded!

 

thanks

 

tmfl

Link to comment
Share on other sites

For the most part, I think your OO approach is correct.  However, I would probably break the validation of the form data out from the class.  In short, handle validation of form data in a PHP page/script and handle argument validation within the class.  This will allow you to send appropriate messages back to the browser for missing/mal-formed/incomplete form entries while your classes manage their attributes and arguments as if they come from a script. 

Link to comment
Share on other sites

You also may want to separate out the login functionality as well, depending on what logging in actually entails, and how smart you want your User object to be.  A classic scenario is for a Login object, or a full-fledged User Manager object responsible for User CRUD, to return a populated User object.

 

But yes, form validation has nothing to do with a User and should be a separate mechanism.

Link to comment
Share on other sites

Thanks for the replies.

 

So you're saying there is no real requirement for a validation class  ? I should just validate the form variables before I pass to the User object login method ?

 

I'm still a bit confused over what the constructor will do too....if I use this class to create a user it will have maybe 3 or more parameters but when a user logs in  it will have only 2.....

eg.

 

What should I do when I'm declaring the constructor in this situation ?

 

Many thanks in advance for anyone providing help here....

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.