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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.