Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3947984
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:19:14+00:00 2026-05-20T01:19:14+00:00

Hey there, here is a question for you guys. I have so much times

  • 0

Hey there,
here is a question for you guys.

I have so much times to choose a error handling for classes in PHP.

For Example in Ajax PHP Handling Classes i do it this way:

public function setError($msg) {
    $this->errors[] = $msg;
}

public function isFailed() {
    return (count($errors) > 0 ? true : false); // if errors > 0 the request is failed
}

public function getJsonResp() {
    if($this->isFailed()) {
        $resp = array('result' => false, 'message' => $this->errors[0]);
    } else {
        $resp = array('result' => true);
        array_merge($resp, $this->success_data); // the success data is set later
    }
    return json_encode($resp);
}

// an example function for a execution of a method would be this

public function switchMethod($method) {
    switch($method) {
        case 'create':
            if(!isset($param1, $param2)) {
                $this->setError('param1 or param2 not found');
            } else {
                $this->createSomething();
            }
            break;
        default:
            $this->setError('Method not found');
    }
}

So lets know you what i want to aks for:
Is there a better solution for error handling?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T01:19:15+00:00Added an answer on May 20, 2026 at 1:19 am

    When it comes to OOP Your best bet is to use Exceptions to handle your errors, for example:

    class Example extends BaseExample implements IExample
    {
        public function getExamples()
        {
            if($this->ExamplesReady === false)
            {
                throw new ExampleException("Examples are not ready.");
            }
        }
    }
    
    class ExampleException extends Exception{}
    

    throwing exceptions within your class and catching exceptions outside of the classes that throw them is the way I usually go about things.

    Usage Example:

    $Example = new Example();
    try
    {
        $Examples = $Example->getExamples();
    
        foreach($Examples as $Example)
        {
            //...
        }
    }catch(ExampleException $e)
    {
        Registry::get("Output")->displayError("Unable to perform action",$e);
    }
    

    and your displayError would use $e->getMessage() as the information regarding the error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys, (relatively, I believe) simple question here, I have a UITableViewController with its
Hey guys here is my question. im using JCombobox. if a student have CB
Hey, a beginner's question here.... In order to learn PHP and SQl I have
Hey guys i have a question here, How do i create a body that
Hey guys this is a followup to my previous question . I now have
Hey guys, I have a form and there are 4 buttons possible for the
Hey guys, my question is about persisting an entity in JDO. I have created
Hey all. I have a question regarding PHP's If shorthand. Is it possible with
Hey there, i have one question.. i try to write an android app with
Hey guys, here's my requirement: I have one central App which receives requests from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.