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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:22:08+00:00 2026-06-10T07:22:08+00:00

Is there a way to create a class’s constants dynamically? I know this sounds

  • 0

Is there a way to create a class’s constants dynamically? I know this sounds a bit odd but let me explain what I’m trying to do:

  • I have a Enum class who’s attributes are defined by static const definitions
  • This class extends the PHP SplEnum class
  • Rather than type in each of these definitions in code I’d like to have a static initialiser go out to the database and pull the enumerated values

Maybe somethings like this:

class myEnum extends SplEnum {
    public static function init () {
        $myNameValuePair = DB_Functions::get_enum_list();
        foreach ( $myNameValuePair as $name => $value) {
            $const = array ( self , $name );
            $const = $value;
        }
    }
}

I recognise that this won’t actually work as it doesn’t set CONST’s but rather static variables. Maybe my whole idea is hair brained and there’s a better technique to this. Anyway, any method to achieve the end goal is greatly appreciated.

UPDATE

I think it might be helpful to be a little more clear on my goals because I think it’s entirely possibly that my use of Constants is not a good one. Basically I want to achieve is typical of the Enumerated list’s requirements:

  1. Constrain function signatures. I want to be able to ask for a “set” of values as an input to a function. For instance:

    public function do_something ( ENUM_Types $type ) {}

  2. Simple and Compact. Allow for a simple and compact syntax when used in code. For instance with the use of constants I might write a conditional statement something like:

    if ( $my_var === ENUM_Types::TypeA ) {}

  3. Dynamic enumeration. I’d like this enumeration to be managed through the frontend and stored in the database (I’m using wordpress admin screens for this in case anyone cares). At run time this “list” should be pulled out of the DB and made available to the code as an enumeration (or similar structure that achieves the goals above).

  • 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-10T07:22:10+00:00Added an answer on June 10, 2026 at 7:22 am

    Wrap your “enum” values in a singleton and implement the (non-static) magic __get method:

    <?php
    class DynamicEnums {
    
      private static $singleton;
    
      private $enum_values;
    
      public static function singleton() {
        if (!self::$singleton) {
            self::$singleton = new DynamicEnums();
        }
        return self::$singleton;
      }
    
      function __construct() {
        $this->enum_values = array( //fetch from somewhere
            'one' => 'two',
            'buckle' => 'my shoe!',
        );
      }
    
      function __get($name) {
        return $this->enum_values[$name]; //or throw Exception?
      }
    
      public static function values() {
        return self::singleton()->enum_values; //warning... mutable!
      }
    }
    

    For bonus points, create a (non-OO) function that returns the singleton:

    function DynamicEnums() {
        return DynamicEnums::singleton();
    }
    

    Consumers of “DynamicEnums” would look like:

    echo DynamicEnums::singleton()->one;
    echo DynamicEnums()->one;            //can you feel the magic?
    print_r(DynamicEnums::values());
    

    [edit] More enum-like.

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

Sidebar

Related Questions

Is there a way to dynamically and conditionally create a class definition in PHP,
Is there a way to create a class that derives from XElement , but
Is there any way to create something similar to this: class F[A] {def apply(a:
Is there a way to create a only instance in a class? At this
Is there any way to create border class with buttons on it? Like this:
Using the standard VS IDE, is there a fast way to create class properties
Is there a way to create a class diagram without using doxygen?
Is there a way to create a new class from a String variable in
Is there an easy way to create a class that is derived from IDispatch
Is there a way to create a System.Windows.Forms.BindingSource from an internal class? Or would

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.