Jump to content

Learning classes - when to error check / exceptions


BahBah

Recommended Posts

Hello

 

I am learning classes, as a long term procedural programmer.

 

To do so I am trying to build myself a user class for authentication.

 

I am finding myself doing error checking in my user class in for example:

 

class User {
  function create ($userData) {
    if ( strlen($userData['username']) < MIN_LENGTH || strlen($userData['username']) > 255 ) {
      return true;
    } else {
      return false;
    }
  }
}

 

What concerns me, is should this error checking be done in the class ? Or should a class be used to perform the function, in this example to create a user. Then the actual error checking should be done either in a seperate class or in the same file as a form processor ?

 

I think I've got myself confused having read up on exceptions. It sounded to me as if exceptions should be handled in classes, and error checking shouldn't be. It is very likely that my interpretation of that is wrong though.

 

Thank you for any assistance.

Link to comment
Share on other sites

Inheritance can help you here.  You can write a validate() method and your create() method can just call validate().

 

If a specific application needs to expand on your basic User object, it can create a new class that extends it and override your pre-existing validate() method.

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.