Dynamic variables in PHP from enum Related to the question Verb conjugator for French, I was asked an question on whether one could summarize all the $exceptionIs&lt;NAME_OF_EXCEPTION&gt; = $exceptionmodel-&gt; getValue() === ExceptionModel::NAME_OF_EXCEPTION lines. In other words, can one make dynamic variables out of the comparison of a value combined with all values of an enum?

As I'm a bit rusty in php, I made this version, and ask you whether this is a good solution, or if it needs major refactoring. To avoid posting other peoples code, I've mocked the ExceptionModel class, and replace the output of ExceptionModel::getConstants() with a predefined array. This to give you working code to review.  The original code is located on github, as the classes ExceptionModel and Enum.

&lt;?php
// A mockup of the original ExceptionModel inheriting from Enum
class ExceptionModel {
    const NO_EXCEPTIONS = 'no_exception';
    const ALLER = 'aller';
    const AVOIR_IRR = 'avoir_irr';
    const ETRE_IRR = 'etre_irr';

    // ... many more lines ...

    function getConstants() {
        // ... returns array of constants ...
    }
}


function myFunction($exception) {
    // In final version, it should use the following line
    // $exceptionModels = ExceptionModel::getConstants();

    // ... but for now, use this array
    $exceptionModels = array (
        "NO_EXCEPTIONS" =&gt; 'no_exception',
        "ALLER" =&gt; 'aller',
        "AVOIR_IRR" =&gt; 'avoir_irr',
        "ETRE_IRR" =&gt; 'etre_irr'
    );

    // Generate dynamic variables testing for equality of
    // of $exception and an Enum value from ExceptionModel
    foreach ($exceptionModels as $constName =&gt; $constValue) {
        ${'exceptionIs' . $constName} = $exception === $constValue;
    }

    if ($exceptionIsALLER) {
        echo "ExceptionModel is aller. ";
    }

    else if ($exceptionIsAVOIR_IRR) {
        echo "ExceptionModel is avoir_irr";
    } else {
        echo "ExceptionModel was neither, it is: " . $exception;
    }

    echo "\n";
}

echo "&lt;pre&gt;";
myFunction("none");
myFunction(ExceptionModel::ALLER);
myFunction(ExceptionModel::AVOIR_IRR);
echo "&lt;/pre&gt;";
?&gt;


This correctly produces the output:



ExceptionModel was neither, it is: none
ExceptionModel is aller. 
ExceptionModel is avoir_irr
PHP allows computed variable names, but it's nearly always a bad idea.  Unless you are careful about variable naming conventions, you could easily trash a variable accidentally, possibly introducing a security vulnerability.

You would be better off using an associative array.

// Generate dynamic variables testing for equality of
// of $exception and an Enum value from ExceptionModel
foreach ($exceptionModels as $constName =&gt; $constValue) {
    $detected[$constName] = $exception === $constValue;
}

if ($detected['ALLER']) {
    echo "ExceptionModel is aller.";
} else if ($detected['AVOIR_IRR']) {
    echo "ExceptionModel is avoir_irr.";
} else {
    echo "ExceptionModel was neither, it is: " . $exception;
}