Dynamic constants in PHP?
<p>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:</p>

<ul>
<li>I have a Enum class who's attributes are defined by static <code>const</code> definitions</li>
<li>This class extends the PHP SplEnum class</li>
<li>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</li>
</ul>

<p>Maybe somethings like this:</p>

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

<p>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.</p>

<p><strong>UPDATE</strong> </p>

<p>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:</p>

<ol>
<li><p><strong>Constrain function signatures</strong>. I want to be able to ask for a "set" of values as an input to a function. For instance:</p>

<p>public function do_something ( ENUM_Types $type ) {}</p></li>
<li><p><strong>Simple and Compact</strong>. 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:</p>

<p>if ( $my_var === ENUM_Types::TypeA ) {}</p></li>
<li><p><strong>Dynamic enumeration</strong>. 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).</p></li>
</ol>
