Question

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?

Was it helpful?

Solution 2

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?

OTHER TIPS

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] . ".");   
} 

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top