tags:

views:

44

answers:

1

My PHP web application is divided into modules, and I use the data model and data mapper patterns. My mapper methods are static, and they exist specifically to interact with the database.

I have the following method:

ModuleMapper::getRecordCountByModuleIdAndSiteId($moduleId, $siteId)

This method is only meant for a set list of modules (I have a good reason). Would it be reasonable to throw an exception if this method is called for a module outside of that set list (eg. "Record count for this module cannot be retrieved.").

try {
  $recordCount = ModuleMapper::getRecordCountByModuleIdAndSiteId($moduleId, $siteId);
}
catch (Exception $e) {
  // handle exception
}

Another option would be to just return 0.

+1  A: 

Depends on how you want to handle errors really. I use zend framework which automatically catches exceptions and forwards them to the error controller for pretty display and logging. Obviously that method will kill processing unless you catch it explicitly. But it simplifies error display and logging.

You could just as well return 0 and do an if statement to display an inline message, or catch the error and display an inline message as well.

Mark