1

I am writing a class to download a csv file with data from the db.
The methodology I used is as follows
1. A main method which calls other methods
eg:

public function main(){
    $this->a();
    $this->b();
    $this->c();
}

suppose method a is validation, b is query execution and c is file download. These 3 methods have chances of producing error eg: error in query execution, error in download etc

when ever I have error in any of these methods I have to return back and display the error. What is the best way to handle this?

1 Answer 1

1

If you give each method a return value, you can return a zero if the method had no errors or a nonzero value representing your error if an error occurred. Have your main function examine the result from each method to see if any errors occurred, and if so, display them. The code for capturing the return value which indicates whether or not you had an error might look like:

public function main(){
    if ($this->a() != 0) {
        // Display the error represented by the return code
    }
    if ($this->b() != 0) {
        // Display the error represented by the return code
    }        
    if ($this->c() != 0) {
        // Display the error represented by the return code
    }
Sign up to request clarification or add additional context in comments.

2 Comments

can we do this with the exception? If possible explain a bit. Am not good in exception handling code.
I'm not entirely sure what you're asking, but you can both create your own tests for successful execution as well as capture errors. To handle exceptions, you can always use try - catch blocks so you get to handle the error instead of allowing PHP to error out for you. In this way, you get to decide the action taken when an error occurs, instead of having your program crash at that point unpredictably.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.