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

  • Home
  • SEARCH
  • 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 699583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:24:08+00:00 2026-05-14T03:24:08+00:00

I’m working on a class-based php web app. I have some places where objects

  • 0

I’m working on a class-based php web app. I have some places where objects are interacting, and I have certain situations where I’m using error codes to communicate to the end user — typically when form values are missing or invalid. These are situations where exceptions are unwarranted ( and I’m not sure I could avoid the situations with exceptions anyways).

In one object, I have some 20 code numbers, each of which correspond to a user-facing message, and a admin/developer-facing message, so both parties know what’s going on. Now that I’ve worked over the code several times, I find that it’s difficult to quickly figure out what code numbers in the series I’ve already used, so I accidentally create conflicting code numbers. For instance, I just did that today with 12, 13, 14 and 15.

How can I better organize this so I don’t create conflicting error codes? Should I create one singleton class, errorCodes, that has a master list of all error codes for all classes, systematizing them across the whole web app? Or should each object have its own set of error codes, when appropriate, and I just keep a list in the commentary of the object, to use and update that as I go along?



Edit: So I’m liking the suggestions to use constants or named constants within the class. That gives me a single place where I programatically define and keep track of error codes and their messages.

The next question: what kind of interface do I provide to the outside world for this class’ error codes and messages? Do I do something like triggerError(20) in the class, and then provide a public method to return the error code, the string constant, and the user- and admin-facing message?

  • 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-14T03:24:08+00:00Added an answer on May 14, 2026 at 3:24 am

    You could create a couple of defines to create named constants for all your error codes :

    define('ERROR_CODE_SQL_QUERY', 1);
    define('ERROR_CODE_PAGE_NOT_FOUND', 2);
    define('ERROR_CODE_NOT_ALLOWED', 3);
    // and so on
    

    And, then, use the constants in your code :

    if ($errorCode == ERROR_CODE_SQL_QUERY) {
        // deal with SQL errors
    }
    

    With that, nowhere in your code you’ll use the numerical value : everywhere *(except in the on and only file where you put the `define`s)*, you’ll use the codes.

    It means :

    • Less risk of errors, as all numerical values are set in only one file
    • Less risk of errors, as you’ll use the constants, that have a name which indicates what it means
    • And code that’s easier to read.

    Another idea could be to create a class to deal with errors :

    class Error {
        const CODE_SQL_QUERY = 1;
        const CODE_PAGE_NOT_FOUND = 2;
        const CODE_NOT_ALLOWED = 3;
        
        // Add some methods here, if needed
    }
    

    And, then, use something like this :

    if ($errorCode == Error::CODE_SQL_QUERY) {
        // deal with SQL errors
    }
    

    Which one is *the best* ?

    It’s probably a matter of personnal preferences… If you need to add some methods to deal with the errors, using a class might be useful. Else, defines are a great solution too.

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

Sidebar

Ask A Question

Stats

  • Questions 356k
  • Answers 356k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer mongrel is quite outdated and doesn't always work with newer… May 14, 2026 at 8:54 am
  • Editorial Team
    Editorial Team added an answer Write the query as: select rooms.room_id, rooms.user_id, users.user_name from rooms… May 14, 2026 at 8:54 am
  • Editorial Team
    Editorial Team added an answer You need a mapping number -> type somewhere, but you… May 14, 2026 at 8:54 am

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.