Related to the question Verb conjugator for French Verb conjugator for French, I was asked an question on whether one could summarize all the $exceptionIs<NAME_OF_EXCEPTION> = $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?
Related to the question Verb conjugator for French, I was asked an question on whether one could summarize all the $exceptionIs<NAME_OF_EXCEPTION> = $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?
Related to the question Verb conjugator for French, I was asked an question on whether one could summarize all the $exceptionIs<NAME_OF_EXCEPTION> = $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?
<?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" => '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) {
${'exception_is_''exceptionIs' . $constName} = $exception === $constValue;
}
if ($exception_is_ALLER$exceptionIsALLER) {
echo "ExceptionModel is aller. ";
}
else if ($exception_is_AVOIR_IRR$exceptionIsAVOIR_IRR) {
echo "ExceptionModel is avoir_irr";
} else {
echo "ExceptionModel was neither, it is: " . $exception;
}
echo "\n";
}
echo "<pre>";
myFunction("none");
myFunction(ExceptionModel::ALLER);
myFunction(ExceptionModel::AVOIR_IRR);
echo "</pre>";
?>
<?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" => '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) {
${'exception_is_' . $constName} = $exception === $constValue;
}
if ($exception_is_ALLER) {
echo "ExceptionModel is aller. ";
}
else if ($exception_is_AVOIR_IRR) {
echo "ExceptionModel is avoir_irr";
} else {
echo "ExceptionModel was neither, it is: " . $exception;
}
echo "\n";
}
echo "<pre>";
myFunction("none");
myFunction(ExceptionModel::ALLER);
myFunction(ExceptionModel::AVOIR_IRR);
echo "</pre>";
?>
<?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" => '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 "<pre>";
myFunction("none");
myFunction(ExceptionModel::ALLER);
myFunction(ExceptionModel::AVOIR_IRR);
echo "</pre>";
?>
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<NAME_OF_EXCEPTION> = $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.
<?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" => '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) {
${'exception_is_' . $constName} = $exception === $constValue;
}
if ($exception_is_ALLER) {
echo "ExceptionModel is aller. ";
}
else if ($exception_is_AVOIR_IRR) {
echo "ExceptionModel is avoir_irr";
} else {
echo "ExceptionModel was neither, it is: " . $exception;
}
echo "\n";
}
echo "<pre>";
myFunction("none");
myFunction(ExceptionModel::ALLER);
myFunction(ExceptionModel::AVOIR_IRR);
echo "</pre>";
?>
This correctly produces the output:
ExceptionModel was neither, it is: none
ExceptionModel is aller.
ExceptionModel is avoir_irr