Jump to content

Error Handling in PHP. How to do it?


lopes_andre

Recommended Posts

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.

 

Link to comment
Share on other sites

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/

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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