BahBah Posted December 21, 2009 Share Posted December 21, 2009 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 https://forums.phpfreaks.com/topic/185943-learning-classes-when-to-error-check-exceptions/ Share on other sites More sharing options...
roopurt18 Posted December 22, 2009 Share Posted December 22, 2009 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 https://forums.phpfreaks.com/topic/185943-learning-classes-when-to-error-check-exceptions/#findComment-981943 Share on other sites More sharing options...
roopurt18 Posted December 22, 2009 Share Posted December 22, 2009 As far as exceptions are concerned, you might make that a configuration flag for your classes. Sometimes it's useful to have third party classes throw Exceptions; sometimes it's not. Link to comment https://forums.phpfreaks.com/topic/185943-learning-classes-when-to-error-check-exceptions/#findComment-981944 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.