tags:

views:

26

answers:

1

I'm getting the error Use of undefined constant NORMAL - assumed 'NORMAL' when running my code, I can't find anything wrong with th code

var $type;

    const NORMAL=1;
    const ADMIN=2;

    public function getTypeOptions(){
        return array(
            self::NORMAL=>'Normal',
            self::ADMIN=>'Administrator',
        );
    }

    public function getTypeText(){

        $options[NORMAL];
        $options[ADMIN];

        $options=$this->getTypeOptions();
        return $options;
    }

    public function getTypeByText($type){
        $options = $this->getTypeText();
        if($type == 1){
            return $options[1];
        }else if($type == 2){
            return $options[2];
        }
    }

I get this everytime I call the getTypeByText method the value of type = 2

+1  A: 

your referencing a global constant, you need to reference a local constant.

$options[self::NORMAL];
$options[self::ADMIN];

should work

Just a note, looking at the way your class::method is built, you don't actually need the above, the below will work just fine.

public function getTypeText()
{
    return $this->getTypeOptions();
}
RobertPitt
@RobertPitt, yip you are correct.
Roland
Wouldn't that make `getTypeText()` completely redundant since they're just synonyms (which by their method names they *shouldn't* be)?
BoltClock
regardless of my change, there still pointless as the method was already redundant.
RobertPitt