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?

9
  • How are you planning to refer to what constant it is you need the ID from? Commented Dec 4, 2013 at 15:36
  • 1
    Check this post stackoverflow.com/questions/956401/… Commented Dec 4, 2013 at 15:36
  • @h2ooooooo like this AClass::CODE_NAME_123 Commented Dec 4, 2013 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? Commented Dec 4, 2013 at 15:41
  • @h2ooooooo not a string ofc Commented Dec 4, 2013 at 15:41

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

4 Comments

Beat me to it by a second Wayne :-)
In case anyone needs to see further usage of the ReflectionClass, I'd recommend checking out the unit tests: github.com/php/php-src/tree/…
..that said, it still doesn't do what OP apparently wants: "just the one with a specific value"
@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.
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

Comments

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
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.