I have a php class with some square constants showing an example status.
When I am using class, after I run some methods, I do some check to ensure that the situation is what I expect.
For example, after calling some methods, I hope this status should be MEANINGFUL_STATUS_NAME
.
$ objInstance-> Method1 (); $ ObjInstance- & gt; Method2 (); If ($ objInstance-> position! == class :: MEANINGFUL_STATUS_NAME) {throw a new exception ("the situation is incorrect, should not be." Class :: MEANINGFUL_STATUS_NAME. "."); }
However, it gives me an exception message
"Status is incorrect, must be 2"
When I really want to see
"the situation is wrong, should not be MEANINGFUL_STATUS_NAME"
That's why I've lost the sense of continuity. I was thinking of creating a 'translation table' array, so I can assume constant and translate them back into their name, but it seems cumbersome that I should translate this back, so I got an error message Does that give me a better idea of being wrong?
This is a difficult solution:
$ r = new reflection ("YourClassName") ; $ ConstantNames = array_flip ($ r-> getConstants ()); $ ObjInstance- & gt; Method1 (); $ ObjInstance- & gt; Method2 (); If ($ objInstance- & gt; status! == YourClassName :: MEANINGFUL_STATUS_NAME) {new exception ("the situation is incorrect, should not be." $ Constant name [yourClassName :: MEANINGFUL_STATUS_NAME]. "."); }
Comments
Post a Comment