Get class constant names in php?

Posted by user151841 on Stack Overflow See other posts from Stack Overflow or by user151841
Published on 2010-05-18T14:41:39Z Indexed on 2010/05/18 14:50 UTC
Read the original article Hit count: 162

Filed under:
|
|
|

I have a php class with some class constants that indicate the status of an instance.

When I'm using the class, after I run some methods on it, I do some checks to make sure that the status is what I expect it to be.

For instance, after calling some methods, I expect the status to be MEANINGFUL_STATUS_NAME.

$objInstance->method1();
$objInstance->method2();
if ( $objInstance->status !==  class::MEANINGFUL_STATUS_NAME ) { 
    throw new Exception("Status is wrong, should not be " . class::MEANINGFUL_STATUS_NAME . ".");
}

However, this gives me the exception message

"Status is wrong, should not be 2"

when what I really want to see is

"Status is wrong, should not be MEANINGFUL_STATUS_NAME"

So I've lost the meaningfulness of the constant name. I was thinking of making an 'translation table' array, so I can take the constant values and translate them back into their name, but this seems cumbersome. How should I translate this back, so I get an error message that gives me a better idea of what went wrong?

© Stack Overflow or respective owner

Related posts about php

Related posts about class-constants