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 8688695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:26:24+00:00 2026-06-12T23:26:24+00:00

I realise this is a big topic, but I’d appreciate some rough ideas on

  • 0

I realise this is a big topic, but I’d appreciate some rough ideas on the best practice ways for handling errors, particularly in OO PHP (but also interested in good patterns which transcend language specifics). Let’s assume these classes and applications are large scale ones.

I also realise that the most recommended way is to make use of PHPs Exception model, and I’m interested in knowing how the below example might translate into this.

EDIT: what I’m looking for is ideas on how best to handle say the errors generated when validating say form data (not errors with how the method has been called), perhaps the Exception model isn’t for this use case END EDIT

Particularly given that not all errors need to be displayed/used immediately (can multiple exceptions be thown in the same method?), some might need to be logged (that is another question I guess), some might only be relevant if other dependencies are true (or false), and some might just be warnings, some might be critical and some might be statuses/notifications.

The current approach I take is something along the lines of:

class ClassName()
{
    public MethodName( $data, &$errors )
    {
        $e = [];

        // validate content and perform data handling
        // any errors should be added to the array $e

        if( empty( $e ) ) {
            return $processed_data;
        } else {
            $errors = $e;
            return false;
        }
    }
}

// and then calling:
$method_call = ClassName::MethodName( $data, $errors );

if( $method_call ) // do something with the data/display success message etc.
else // decide what to do with any errors

Tar Folks,

  • 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-06-12T23:26:26+00:00Added an answer on June 12, 2026 at 11:26 pm

    It depends really. The “proper” OO way to do that is to use exceptions though. There are already many different types of exception built in (see http://www.php.net/manual/en/spl.exceptions.php) and you can define your own (see http://php.net/manual/en/language.exceptions.php).

    A way to use them could be similar to this

    function foo($bar)
    {
        if(!is_string($bar))
            throw new InvalidArgumentException("String argument expected");
    
        if(strlen($bar) > 50)
            throw new LengthException("String argument is too long!");
    }
    

    Then, in calling code, use try/catch, e.g.

    try {
         foo("hjsdkfjhkvbnsjd");
    } catch(LengthException $ex) {
         // Trim the string
    } catch (InvalidArgumentException $ex) {
         // Try to recover. Cast or trim or something
    } catch (Exception $ex) {
         // We hit an exception we're not explicitly handling.  
         // Gracefully exit
         die($ex->Message);
    }
    

    In any case, an exception should only be thrown if the method has actually been called wrong, or if the method fails in a fatal way.

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

Sidebar

Related Questions

This is a bit of a vague question I realise, but I've been looking
I realise the info to answer this question is probably already on here, but
I realise this might be hard to explain, so let me start by using
So Heroku has been down all day, of course I didn't realise this until
Its when I try to do stuff like this I realise I really need
This question may seem blindingly obvious and I realise I am putting myself up
I stumbled upon this problem and it took my a while to realise what
I realize this is syntactically bad but I figure it somewhat explains what I'm
I realize this is more of a hardware question, but this is also very
I realize this is a rather odd request, but I was wondering if anyone

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.