1

I have a class with constants like this:

class AClass
{
    const CODE_NAME_123 = "value1";
    const CODE_NAME_456 = "value2";
}

Is there a way to convert the name of a constant like AClass::CODE_NAME_123 into a string in order to, e.g., extract the trailing digits from the string?

Desmond Hume
  • 8,037
  • 14
  • 65
  • 112
  • How are you planning to refer to what constant it is you need the ID from? – h2ooooooo Dec 04 '13 at 15:36
  • 1
    Check this post http://stackoverflow.com/questions/956401/can-i-get-consts-defined-on-a-php-class –  Dec 04 '13 at 15:36
  • @h2ooooooo like this `AClass::CODE_NAME_123` – Desmond Hume Dec 04 '13 at 15:39
  • @DesmondHume Is this a string? Why do you need to convert it into a string again, if it already is a string? If you already know the constant name, then what's the problem? – h2ooooooo Dec 04 '13 at 15:41
  • @h2ooooooo not a string ofc – Desmond Hume Dec 04 '13 at 15:41
  • @DesmondHume But you know the constant name. Then you obviously know the number as well? Are you trying to figure out what constant a specific value has? Can you show the code where you tried something that didn't work? Do you want to extract constants from the class? – h2ooooooo Dec 04 '13 at 15:42
  • @h2ooooooo there're dozens of constants of this kind that need to be processed according to their names – Desmond Hume Dec 04 '13 at 15:43
  • @DesmondHume So you want to get the "id"'s of every constant in `AClass` that starts with the string `CODE_NAME_`? – h2ooooooo Dec 04 '13 at 15:44
  • @h2ooooooo not every, just the one with a specific value – Desmond Hume Dec 04 '13 at 15:45

3 Answers3

4

You can use something like this:

//PHP's ReflectionClass will allow us to get the details of a particular class
$r = new ReflectionClass('AClass');
$constants = $r->getConstants();

//all constants are now stored in an array called $constants
var_dump($constants);

//example showing how to get trailing digits from constant names
$digits = array();
foreach($constants as $constantName => $constantValue){
    $exploded = explode("_", $constantName);
    $digits[] = $exploded[2];
}

var_dump($digits);
Wayne Whitty
  • 19,513
  • 7
  • 44
  • 66
  • 1
    Beat me to it by a second Wayne :-) – Homer6 Dec 04 '13 at 15:41
  • 1
    In case anyone needs to see further usage of the ReflectionClass, I'd recommend checking out the unit tests: https://github.com/php/php-src/tree/94e15ff3877f842e5eb5c89e3aeab214fb4a3a33/ext/reflection/tests – Homer6 Dec 04 '13 at 15:46
  • ..that said, it still doesn't do what OP apparently wants: "*just the one with a specific value*" – h2ooooooo Dec 04 '13 at 16:03
  • @h2ooooooo True, but I just answered the question that he asked. Wasn't until later that he moved the goal posts and wanted the trailing digits of a constant with a specific value. – Wayne Whitty Dec 04 '13 at 16:04
1

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

h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
0

Use ReflectionClass() and loop through the resulting keys applying a preg_match to each value to extract the last 3 numbers of the string:

class AClass
{
    const CODE_NAME_123 = "";
    const CODE_NAME_456 = "";
}

$constants = new ReflectionClass('AClass');

$constants_array = $constants->getConstants();

foreach ($constants_array as $constant_key => $constant_value) {
   preg_match('/\d{3}$/i', $constant_key, $matches);
   echo '<pre>';
   print_r($matches);
   echo '</pre>';
}

The output would be:

Array
(
    [0] => 123
)
Array
(
    [0] => 456
)
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103