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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:32:15+00:00 2026-05-22T02:32:15+00:00

Greetings StackOverflow I’m doing some house cleaning and thought I would ask for some

  • 0

Greetings StackOverflow

I’m doing some house cleaning and thought I would ask for some suggested practices in PHP as I re-factor my classes. In particular, I have some class constants which fall into categories and I would like to know some good ways to group the ones which share a common purpose.

An Example:

class MySquare {
  // Colors
  const COLOR_GREEN = "green";
  const COLOR_RED = "red";

  // Widths
  const WIDTH_SMALL = 100;
  const WIDTH_MEDIUM = 500;
  const WIDTH_BIG = 1000;

  // Heights
  const HEIGHT_SMALL = 100;
  const HEIGHT_MEDIUM = 300;
  const HEIGHT_BIG = 500;
}

Obviously this works, but it seems like there are probably plenty of options when it comes to grouping related constants, and I bet this is inferior to most. How would you do it?

  • 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-22T02:32:16+00:00Added an answer on May 22, 2026 at 2:32 am

    There are many PHP Conventions, and all of them contradict. But I do use a similar notation, although I like to group the constants per class, so I would have a class
    Height (or MySquare_Height) which has the constants. This way, I can use it as a kind of Enum like you got in other languages. Especially when you use an editor with highlighting.

    <?
    abstract class MySquare_Color
    {
      const GREEN = 'Green';
      const RED = 'Red';
    }
    
    abstract class MySquare_Height 
    {
      const SMALL = 100;
      const MEDIUM = 300;
      const BIG = 500;
    }
    

    If you’r using PHP 5.3, you can just name the classes Color and Height and put them in a MySquare namespace:

    <?php
    // Namespace MySquare with subnamespace Width containing the constants.
    namespace MySquare\Width
    {
        const SMALL = 100;
        const MEDIUM = 300;
    }
    
    namespace
    {
      // Refer to it fromout another namespace
      echo MySquare\Width\SMALL;
    }
    
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings to all! This is my first question here on stackoverflow. I have a
Greetings! I'm working on wrapping my head around LINQ. If I had some XML
Greetings everyone. This is my first question here at stackoverflow so please bear with
Greetings stackoverflow. I have been tinkering about with a SOAP client using PHP5's native
Greetings, fellow StackOverFlow members. I have just begun to learn how to program web-enabled
Greetings StackOverflow community! For this new project I'm required to write a trigger which
Greetings to the stackoverflow community, I am currently following a bioinformatics module as part
Greetings, There are a lot of such questions on stackoverflow, but all of them
Greetings all. I am writing some code using the Boost Units library and have
Greetings All; I have a LinkedList of type String which contain some words like

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.