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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:39:04+00:00 2026-06-09T20:39:04+00:00

Best Practice to return user information Every time it bothers me; how to return

  • 0

Best Practice to return user information

Every time it bothers me; how to return information (login success, article updated)back to the user especially in OOP.. (PHP)

  • throw new exception with multiple catch and try blocks (after catch it will stop the execution of the catch block)
  • just a bunch return values (just looks awful)
  • true and false (then in the script where you run the class check for true and false?)
  • separated class to handle the error messages (extends Exceptions?)

Any tips would be appreciated!

— Update / not axpecting so many answers! wow!!

For example user registration; I used a custom exception class.

/*
 * custom exception class
 */
class Message extends Exception
{
    public function __construct($message, $code = true) 
    {       
        parent::__construct($message, $code);
    }

    public function information() 
    {
        return '<div class="check"><img src="/admin/static/images/'.($this->code == true ? 'good' : 'bad').'.gif" alt="check" class="icon" width="20px" height="20px" />' . $this->message . '</div>';
    }
}

and in the class itself;

    if(is_numeric($id) === false)
    {
        Throw new Message('message', false);
    }
  • 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-09T20:39:05+00:00Added an answer on June 9, 2026 at 8:39 pm

    throw new exception with multiple catch and try blocks (after catch it will stop the execution of the catch block)

    Exceptions can be costly also exceptions normally denote a problem with the app itself like a cURL error, not something as simple as just the user making a mistake or not logging in right. I would keep exceptions to actual problems in your app and report only to you on them.

    just a bunch return values (just looks awful)

    Depends on how you do these returns values. Look into how PHP frameworks do it like:

    • CodeIgniter
    • Yii
    • CakePHP
    • Zend
    • Lithium

    In particular ( a nice way of doing this ) is Yii’s own login handler for users: http://www.yiiframework.com/doc/api/1.1/CUserIdentity

    You will notice they have class based error codes and messages.

    You will find a lot of these frameworks use a model/controller based pattern for forms which means you define a set of validation rules (even for logging in a user, as in Yii many times the auth is class based) and these validation rules run filling up error messages attached to these rules. Eventually you have an array like:

    array{
        'username' => 'Username was empty',
        'password' => array(
            // More errors
        )
    }
    

    Which denotes all your return values. It is nice and tidy since it is per field errors and they are also easy to get to and manipulate in a loop.

    For actually logging in a user many times you would run something along the lines of:

    if($user->authenticate()){
        // redirect to member page
    }else{
        return $user->getErrors();
    }
    

    And the return of this would normally be something like what was shown above for forms.

    true and false (then in the script where you run the class check for true and false?)

    True and false are very vague when it comes to multiple errors, for example:- the user logs in but their account can be deleted or banned. How would you tell the difference by just boolean?

    Other people/frameworks like Lithium (for example) return boolean per validation function which is then attached to a error message holder.

    As many others say the path comes down to your particular situation.

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

Sidebar

Related Questions

Just wondering what the best practice for handling login/user authentication in mvc3 would be.
What's the best practice for redirecting the user, using Devise, back to the page
What’s the best practice when returning something from a routine? Should I return a
I am mulling over a best practice for passing hash references for return data
What would be the best practice to keep the user logged in if he
What is the best practice for generating valid XML with PHP from user submitted
what is the best practice for this scenario: 1) User clicks Sort huge javascript
I'm wondering what the best practice is in reporting back to the browser about
What would be the best practice regarding integrity if a user uploads user data
What's the best practise when adapting C-style functions which return a true/false to Java?

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.