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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:54:32+00:00 2026-05-17T19:54:32+00:00

This question: Best way to return status flag and message from a method in

  • 0

This question: Best way to return status flag and message from a method in Java is similar to mine, however I would do it in PHP, not Java (which could make a slight difference here).

The problem:

There’s a method that can have either a successful outcome (this may change to be more successful ones) or a “problematic” one. The latter means the operation failed, but it is also important to know why.
Imagine a method of an Authentication class:

public function login($name, $password)
{
    if (successful_authentication)
    {
        report success
    }
    else
    {
        report failure, explain why (e.g. wrong name/pass, user banned)
    }
}

It would be trivial to return true and false for success and failure, but how to report the cause of failure?

Possible solutions:

  • Return true or false and write another method (getStatus()) to get the specific problem: this feels a bit awkward to me
  • Use exceptions: since it’s not exceptional for a user to be banned (an exception would be if the user would die while typing as another author on this site has pointed out) the usage of exceptions here in these cases would be plain wrong (however the method could throw a database exception if a query fails)
  • Return true on success and a string on failure with an error code indicating the problem: with PHP it’s possible this way to have nice clean blocks as follows:

    $loginStatus = $auth->login('name', 'pass');
    if ($loginStatus === true)
    {
        doSomething();
    }
    else
    {
        if ($loginStatus == 'wrong_login_data')
        ...
        elseif ($loginStatus == 'banned')
        ...
        // or with an array full of error messages:
        echo $erroMessages[$loginStatus];
    }
    
  • Return a general status (state) object: very elegant solution and also future-proof (no problem if the number of statuses varies or later additional data should be returned), maybe the best one:

    $loginStatus = $auth->login('name', 'pass')
    if ($loginStatus->isSuccess)
    ...
    else
        echo $errorMessages[$loginStatus->errorCode]; // $errorMessages as by the previous example
    
  • Either of the above two, but not with plain string but class constants:

    $loginStatus = $auth->login('name', 'pass')
    if ($loginStatus->isSuccess)
    ...
    elseif ($loginStatus->errorCode == Auth::ERR_USER_BANNED)
    ...
    

    With this it would be unnecessary to explain the error codes in the documentation and would feel more “natural” as well (at least for me).

The question:

What would you use (of the above ones or any other solution)? What have been proven on the long run to be a good way?

Thank you in advance!

  • 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-17T19:54:33+00:00Added an answer on May 17, 2026 at 7:54 pm

    This is how PEAR has been doing it for as long as I can remember. So this is a tried and tested method.

    class LoginError {
        private $reason;
    
        public function __construct($reason)
        {
            $this->reason = $reason;
        }
    
        public function getReason()
        {
            return $this->reason;
        }
    }
    
    function isError($response)
    {
        return ($response instanceof LoginError);
    }
    
    public function login($name, $password)
    {
        if (successful_authentication)
        {
            return true;
        }
        else
        {
            return new LoginError('The password is incorrect');
        }
    }
    
    
    $loginStatus = login('name', 'pass');
    if (!isError($loginStatus))
    {
        doSomething();
    }
    else
    {
        echo $loginStatus->getReason();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question is about memory use and objects in actionscript 2. If I have
This is beyond both making sense and my control. That being said here is
I have found this example on StackOverflow: var people = new List<Person> { new
I want to use a temp directory that will be unique to this build.
(please excuse that I didn't use aliases). I would like my query output to
After having read Ian Boyd 's constructor series questions ( 1 , 2 ,
I have a new web app that is packaged as a WAR as part
I'm trying to build a C++ extension for python using swig. I've followed the
Let say I have the following desire, to simplify the IConvertible's to allow me
I have a login.jsp page which contains a login form. Once logged in the

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.