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 it and send an e-mail.
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.