2

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?

user151841
  • 17,377
  • 29
  • 109
  • 171
  • This is more or less a repeat of http://stackoverflow.com/questions/255312/how-to-get-a-variable-name-as-a-string-in-php – KingRadical May 18 '10 at 18:49
  • 1
    possible duplicate of [Can I get CONST's defined on a PHP class?](http://stackoverflow.com/questions/956401/can-i-get-consts-defined-on-a-php-class) – outis Sep 26 '11 at 05:48

3 Answers3

6

This is kind of tricky solution:

$r = new ReflectionClass("YourClassName");
$constantNames = array_flip($r->getConstants());

$objInstance->method1();   
$objInstance->method2();   
if ( $objInstance->status !== YourClassName::MEANINGFUL_STATUS_NAME ) {    
    throw new Exception("Status is wrong, should not be " . $constantNames[YourClassName::MEANINGFUL_STATUS_NAME] . ".");   
} 
Zsolti
  • 1,571
  • 1
  • 11
  • 22
1

It occurs to me now that I could use strings as the values for the constants. I'm used to seeing numbers. Is there a reason why I shouldn't do this, or why this wouldn't work?

user151841
  • 17,377
  • 29
  • 109
  • 171
0

Another way might be to let the object check if the status is in a desired mode.

If it is not, the object's method can throw the Exception.

Untested example:

class Klasse
{
    const WANTED_VALUE    =  1;
    const UNWANTED_VALUE  =  2;

    private static $constant_names  =  array();
    p... function __construct ()
    {
        if ( empty( self::$constant_names ) )
        {
            $class            =  new ReflectionClass( __CLASS__ );
            $constants        =  $class->getConstants();
            $constants        =  array_flip( $constants );// requires the constant's values to be unique
            self::$constants  =  $constants;
        }
    }
    public function checkNeededStatus ()
    {
        $needed_status  =  self::WANTED_VALUE;// could also be a parameter(an argument) with a default value
        if ( $this->status !== $needed_status )
        {
            $constant_name  =  self::$constants[ $this->status ];
            $message        =  'Status is wrong, '
                . 'should not be: `' . $constant_name . '`.'
            ;
            //$message .=  '(it should be `' . self::$constants[ $needed_status ] . '`)';
            throw new Exception( $message );
        }
    }
}

$objInstance  =  new Klasse();
$objInstance->method1();
$objInstance->method2();
$objInstance->checkNeededStatus();

credit :

It might be good to consider not using Reflection. Because the thrown message stays inside the class, this has become more likely to do without loosing much maintainability.

Community
  • 1
  • 1
imme
  • 598
  • 1
  • 9
  • 22