How to create a class constant

A class with three class constants

class Person {
  const GREEN_EYES = 1;
  const BLUE_EYES = 2;
  const BROWN_EYES = 3;
  private $eye_color;
  public function getEyeColor() {
    return $this->eye_color;
  } 
  public function setEyeColor($value) {
    if ($value == self::GREEN_EYES || $value == self::BLUE_EYES || $value == self::BROWN_EYES) {
$this->eye_color = $value;
    } else {
exit('Eye Color Not Set');
    }
  }
}

Use the constant outside the class

$person = new Person();
$person->setEyeColor(Person::GREEN_EYES);

Description

Back