<!doctype html>
<?php
  date_default_timezone_set("America/New_York");
?>
<html lang="en">
  <head>
    <title>How to code class constants, properties, and methods</title>
    <link href="https://jimgerland.com/includes/css/styles.css" rel="stylesheet" />
    <link href="https://jimgerland.com/includes/css/codestyles.css" rel="stylesheet" />   
    <style>
      table, th, td {
        font-family: Verdana, Arial, Sans-Serif;
        font-size: large;
      }
    </style>
  </head>
  <body>
    <h1>How to create a class constant</h1>
    <h2>A class with three class constants</h2>
    <div style="font-family: Courier, Arial, Sans-Serif; font-size: large;">
<pre>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');
    }
  }
}</pre>
    </div>
    <h2>Use the constant outside the class</h2>
    <div style="font-family: Courier, Arial, Sans-Serif; font-size: large;">
<pre>$person = new Person();
$person->setEyeColor(Person::GREEN_EYES);</pre>
    </div>
    <div style="font-family: Courier, Arial, Sans-Serif; font-size: large;">
    <h2>Description</h2>
      <ul>
        <li>A <em>class constant</em> is a constant value that belongs to the class, not to objects created from the class.</li>
        <li>To access a constant that belongs to a class, you can code the name of the class followed by a double colon. This double colon is known as the <em>scope resolut operator</e>. It is also known as the <em>Paamayim Nekudotayim</em>, which means double colon in Hebrew.</li>
        <li>Inside a class, you can access a class constant by coding the self keyword followed by a double colon and the class constant name.</li>
        <li>Outside the class, you can access a class constant by coding the class name followed by a double colon and the class constant name.</li>
        <li>Class constants are always public. You can’t use the public, private, or protected keywords with them.</li>
        <li>Class constants are typically used for defining a set of options that are passed to methods in the class.</li>
      </ul>
    </div>
    <p><a href="view_files.php">Back</a></p>
  </body>
</html> 