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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:28:23+00:00 2026-05-10T19:28:23+00:00

I know that PHP doesn’t yet have native Enumerations. But I have become accustomed

  • 0

I know that PHP doesn’t yet have native Enumerations. But I have become accustomed to them from the Java world. I would love to use enums as a way to give predefined values which IDEs’ auto-completion features could understand.

Constants do the trick, but there’s the namespace collision problem and (or actually because) they’re global. Arrays don’t have the namespace problem, but they’re too vague, they can be overwritten at runtime and IDEs rarely know how to autofill their keys without additional static analysis annotations or attributes.

Are there any solutions/workarounds you commonly use? Does anyone recall whether the PHP guys have had any thoughts or decisions around enumerations?

  • 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. 2026-05-10T19:28:23+00:00Added an answer on May 10, 2026 at 7:28 pm

    Since PHP 8.1, Enums are supported:

    https://www.php.net/manual/en/language.types.enumerations.php

    enum DaysOfWeek: int {     case Sunday = 0;     case Monday = 1;     // etc. } $today = DaysOfWeek::Sunday; var_dump($today->value); // 0 var_dump($today->name); // "Sunday" 

    PHP 8.0 and earlier

    Depending upon use case, I would normally use something simple like the following:

    abstract class DaysOfWeek {     const Sunday = 0;     const Monday = 1;     // etc. }  $today = DaysOfWeek::Sunday; 

    However, other use cases may require more validation of constants and values. Based on the comments below about reflection, and a few other notes, here’s an expanded example which may better serve a much wider range of cases:

    abstract class BasicEnum {     private static $constCacheArray = NULL;      private static function getConstants() {         if (self::$constCacheArray == NULL) {             self::$constCacheArray = [];         }         $calledClass = get_called_class();         if (!array_key_exists($calledClass, self::$constCacheArray)) {             $reflect = new ReflectionClass($calledClass);             self::$constCacheArray[$calledClass] = $reflect->getConstants();         }         return self::$constCacheArray[$calledClass];     }      public static function isValidName($name, $strict = false) {         $constants = self::getConstants();          if ($strict) {             return array_key_exists($name, $constants);         }          $keys = array_map('strtolower', array_keys($constants));         return in_array(strtolower($name), $keys);     }      public static function isValidValue($value, $strict = true) {         $values = array_values(self::getConstants());         return in_array($value, $values, $strict);     } } 

    By creating a simple enum class that extends BasicEnum, you now have the ability to use methods thusly for simple input validation:

    abstract class DaysOfWeek extends BasicEnum {     const Sunday = 0;     const Monday = 1;     const Tuesday = 2;     const Wednesday = 3;     const Thursday = 4;     const Friday = 5;     const Saturday = 6; }  DaysOfWeek::isValidName('Humpday');                  // false DaysOfWeek::isValidName('Monday');                   // true DaysOfWeek::isValidName('monday');                   // true DaysOfWeek::isValidName('monday', $strict = true);   // false DaysOfWeek::isValidName(0);                          // false  DaysOfWeek::isValidValue(0);                         // true DaysOfWeek::isValidValue(5);                         // true DaysOfWeek::isValidValue(7);                         // false DaysOfWeek::isValidValue('Friday');                  // false 

    As a side note, any time I use reflection at least once on a static/const class where the data won’t change (such as in an enum), I cache the results of those reflection calls, since using fresh reflection objects each time will eventually have a noticeable performance impact (Stored in an assocciative array for multiple enums).

    Now that most people have finally upgraded to at least 5.3, and SplEnum is available, that is certainly a viable option as well–as long as you don’t mind the traditionally unintuitive notion of having actual enum instantiations throughout your codebase. In the above example, BasicEnum and DaysOfWeek cannot be instantiated at all, nor should they be.

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

Sidebar

Ask A Question

Stats

  • Questions 75k
  • Answers 75k
  • 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
  • added an answer If you're running this from multiple threads, you'll need to… May 11, 2026 at 2:38 pm
  • added an answer I found a blog entry that has a solution in… May 11, 2026 at 2:38 pm
  • added an answer This appears to be a bug in the library. Do… May 11, 2026 at 2:38 pm

Related Questions

I know that PHP doesn't yet have native Enumerations. But I have become accustomed
I recently had my first encounter with PHP (5) through a Drupal application for
I wrote a small PHP application that I'd like to distribute. I'm looking for
I'm writing a very simple flash app (AS 2) to sign users up to
I'm trying to implement a stateful web service in PHP using the SOAP extension.

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.