You can use ReflectionClass::getConstants()
and iterate through the result to find a constant with a specific value, and then get the last digits from the constant name with regex:
<?php
class AClass
{
const CODE_NAME_123 = "foo";
const CODE_NAME_456 = "bar";
}
function findConstantWithValue($class, $searchValue) {
$reflectionClass = new ReflectionClass($class);
foreach ($reflectionClass->getConstants() as $constant => $value) {
if ($value === $searchValue) {
return $constant;
}
}
return null;
}
function findConstantDigitsWithValue($class, $searchValue) {
$constant = findConstantWithValue($class, $searchValue);
if ($constant !== null && preg_match('/\d+$/', $constant, $matches)) {
return $matches[0];
}
return null;
}
var_dump( findConstantDigitsWithValue('AClass', 'foo') ); //string(3) "123"
var_dump( findConstantDigitsWithValue('AClass', 'bar') ); //string(3) "456"
var_dump( findConstantDigitsWithValue('AClass', 'nop') ); //NULL
?>
DEMO