HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Dynamic variables in PHP from enum

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
phpenumvariablesdynamicfrom

Problem

Related to the question Verb conjugator for French, I was asked an question on whether one could summarize all the $exceptionIs = $exceptionmodel-> 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.

 'no_exception',
        "ALLER" => 'aller',
        "AVOIR_IRR" => 'avoir_irr',
        "ETRE_IRR" => 'etre_irr'
    );

    // Generate dynamic variables testing for equality of
    // of $exception and an Enum value from ExceptionModel
    foreach ($exceptionModels as $constName => $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 "";
myFunction("none");
myFunction(ExceptionModel::ALLER);
myFunction(ExceptionModel::AVOIR_IRR);
echo "";
?>


This correctly produces the output:

ExceptionModel was neither, it is: none
ExceptionModel is aller. 
ExceptionModel is avoir_irr

Solution

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 => $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;
}

Code Snippets

// Generate dynamic variables testing for equality of
// of $exception and an Enum value from ExceptionModel
foreach ($exceptionModels as $constName => $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;
}

Context

StackExchange Code Review Q#111939, answer score: 2

Revisions (0)

No revisions yet.