lopes_andre Posted December 22, 2010 Share Posted December 22, 2010 Hi, I'm a beginner in PHP OOP and I'm with some doubts about the correct way of handling errors in PHP. Look at this function for example: public function deleteFileFromDisk($fileNameToBeDeleted) { $handle = unlink($fileNameToBeDeleted); if (!$handle) { $result = "(this->deleteFileFromDisk) - Error, " . $fileNameToBeDeleted . " not deleted."; } else { $result = "(this->deleteFileFromDisk) - Success, " . $fileNameToBeDeleted . " deleted."; } return $result; } Is this the correct way of doing it, or I can do better than this? Let me add some details of what I'm achieving... I'm running class methods, and I need to control errors in the process. If any call to the object throw an error I need to catch, stop all the process and send an e-mail with the error. Here are the object interactions: $testar_classe = new geoIpImportCSV('geolitecity', 'http://geolite.maxmind.com/download/geoip/database/GeoLiteCity_CSV/'); $testar_classe->downloadAndSaveFile('./', $testar_classe->obtainDownloadFileName()); $testar_classe->uncompressZipFile($testar_classe->obtainDownloadFileName(), '.'); $testar_classe->deleteLine(1, 'GeoLiteCity-Location.csv'); $testar_classe->deleteLine(1, 'GeoLiteCity-Blocks.csv'); $testar_classe->deleteDataFromTable('tabela1'); $testar_classe->deleteDataFromTable('tabela2'); $testar_classe->insertLinesToDb('GeoLiteCity-Location.csv', 'tabela1'); $testar_classe->insertLinesToDb('GeoLiteCity-Blocks.csv', 'tabela2'); $testar_classe->deleteFileFromDisk($testar_classe->obtainDownloadFileName()); $testar_classe->deleteFileFromDisk('GeoLiteCity-Blocks.csv'); $testar_classe->deleteFileFromDisk('GeoLiteCity-Location.csv'); Which is the best way of handle this? Create a new method to take care of the exceptions? There are any examples on how to do this? Best Regards. Quote Link to comment https://forums.phpfreaks.com/topic/222395-error-handling-in-php-how-to-do-it/ Share on other sites More sharing options...
nafetski Posted December 22, 2010 Share Posted December 22, 2010 Like many things in PHP error handling (and the output) can be done so many ways there isn't an exact "right" answer. Since this topic is more about theory than handling a specific problem, what I post is definitely open for debate. I remember very clearly when I wanted to start doing things the "right" way - and proper error handling seemed like the most logical place to start. The well goes deeper than that - and here are a few pieces of advice that might help you from a lot of trial & error that I went through. First off...if you're starting down that road of becoming a "real" PHP developer, I'd highly recommend using a framework. I personally love Kohana (and can support the questions very well but really I can't stress enough to use ANY object oriented PHP framework (Kohana, Lithium, CakePHP, Symfony...or countless others). That doesn't really answer your question tho. The real question is "How do I handle errors?!" Well, since you're writing object oriented PHP, the right answer is....have a validation class / use exceptions. First line of defense...REAL validation The best investment you'll make as a developer is having a centralized system for validating/sanitizing user input ... then using THAT interface for 100% of user interaction. If you're using a validation class, you can store those errors / messages / logic away from your application logic. Second line of defense...Exceptions Here is a great article that might help http://www.devshed.com/c/a/PHP/Exception-Handling-in-PHP/ Quote Link to comment https://forums.phpfreaks.com/topic/222395-error-handling-in-php-how-to-do-it/#findComment-1150373 Share on other sites More sharing options...
lopes_andre Posted December 22, 2010 Author Share Posted December 22, 2010 Like many things in PHP error handling (and the output) can be done so many ways there isn't an exact "right" answer. Since this topic is more about theory than handling a specific problem, what I post is definitely open for debate. I remember very clearly when I wanted to start doing things the "right" way - and proper error handling seemed like the most logical place to start. The well goes deeper than that - and here are a few pieces of advice that might help you from a lot of trial & error that I went through. First off...if you're starting down that road of becoming a "real" PHP developer, I'd highly recommend using a framework. I personally love Kohana (and can support the questions very well but really I can't stress enough to use ANY object oriented PHP framework (Kohana, Lithium, CakePHP, Symfony...or countless others). That doesn't really answer your question tho. The real question is "How do I handle errors?!" Well, since you're writing object oriented PHP, the right answer is....have a validation class / use exceptions. First line of defense...REAL validation The best investment you'll make as a developer is having a centralized system for validating/sanitizing user input ... then using THAT interface for 100% of user interaction. If you're using a validation class, you can store those errors / messages / logic away from your application logic. Second line of defense...Exceptions Here is a great article that might help http://www.devshed.com/c/a/PHP/Exception-Handling-in-PHP/ Thanks for the reply! The article is great. Quote Link to comment https://forums.phpfreaks.com/topic/222395-error-handling-in-php-how-to-do-it/#findComment-1150391 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.