0

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

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Elitmiar
  • 35,072
  • 73
  • 180
  • 229
  • See also [my attempt at a canonical answer for causes of this error message](http://stackoverflow.com/questions/2941169/what-does-the-php-error-message-notice-use-of-undefined-constant-mean/8025500#8025500). – John Carter Nov 06 '11 at 06:35

1 Answers1

2

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
  • 56,863
  • 21
  • 114
  • 161
  • Wouldn't that make `getTypeText()` completely redundant since they're just synonyms (which by their method names they *shouldn't* be)? – BoltClock Sep 11 '10 at 11:44
  • regardless of my change, there still pointless as the method was already redundant. – RobertPitt Sep 11 '10 at 11:49