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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:07:49+00:00 2026-06-04T07:07:49+00:00

I have a class that does something, and when it’s done it returns true

  • 0

I have a class that does something, and when it’s done it returns true, but if any of the various things go wrong it will return false. Now my question is, should status messages for such events be kept inside a class (Example1) or outside the class (Example2) where class maybe only provides error code to help distinguishing what happened.

    class Example1 {

       private $var;
       private $status;

       public function doSomething($var) {
         $this->var = $var;

         if (!is_numeric($this->var)) { 
            $this->status = 'Please provide a number'; 
            return false; 
         }

         if (empty($this->var)) {
            $this->status = 'Please fill the field';
            return false;
         }

         $this->status = 'Ok, you submitted a number, cool.';
         return true;
    } 

         function getStatus() {
            return $this->status;
         }
}

example 2:

    class Example2 {

       private $var;

       public function doSomething($var) {
         $this->var = $var;

         if (!is_numeric($this->var)) {  
            return false; 
         }

         if (empty($this->var)) {
                return false;
         }

             return true;
    } 

}

Example 1 seems to me more convenient to use, code reads like a poem, but at the same time seems less reusable, depending on what you use class for, you might want success/error messages to have different syntax.

So basically my question would be what’s the usual practice?

  • 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-04T07:07:50+00:00Added an answer on June 4, 2026 at 7:07 am

    First example: you hard code error messages which is… bad. And you can’t see the error message if you don’t check $object->status later.
    Second example: if something goes wrong, you know it but you don’t know why.

    I suggest avoiding both these ways and throwing exceptions for a more object oriented approach (I guess that’s what you want, since you’re using classes :).

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

Sidebar

Related Questions

I have some code that does something like this: abstract class Data { Data(string
Let's say I have a service class that does something for me and a
So assume I have a class that has an init method that does something
Say I have a template class that takes msgs from source, does something smart
I have a class that does something like: class MyClass(object): def __init__(self, delegate_to): self._delegate_to
I have a static util class that does some string manipulation on a bit
I have a class named toto which I send to a function that does
Say I have a class ObjectA (a view controller for example), that does this
Does anyone have a working class or function to create the hashed email that
I have defined a class that does a lengthy task and I call it

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.