public static function main() { $color1 = Color::BLUE(); $color2 = Color::YELLOW(); $color3 = Color::BLUE(); self::displayColor($color1); self::displayColor($color2); self::displayColor($color3); self::compareColor($color1, $color2); self::compareColor($color2, $color3); self::compareColor($color1, $color3); self::compareColor($color1, $color1); switch ($color1) { case Color::RED(): echo 'red' . "\n"; break; case Color::ORANGE(): echo 'orange' . "\n"; break; case Color::YELLOW(): echo 'yellow' . "\n"; break; case Color::GREEN(): echo 'green' . "\n"; break; case Color::BLUE(): echo 'blue' . "\n"; break; case Color::INDIGO(): echo 'indigo' . "\n"; break; case Color::VIOLET(): echo 'violet' . "\n"; break; } try { $invalidColor = Color::BLACK(); } catch (\BadFunctionCallException $e) { echo 'error : ' . $e->getMessage() . "\n"; } }
/** * Demonstrate type hinting against Enum implementation by saying something nice about the * received Color. * * @param Color $color The color to say something nice about. * * @return void */ function saySomethingNice(Color $color) { switch ($color) { case Color::RED(): $something_nice = ' is like the love of a rose petal.'; break; case Color::BLUE(): $something_nice = ' is like a deep sea on a sunny day.'; break; default: $something_nice = ' is a wonderful color.'; break; } echo 'The color ' . $color->getName() . $something_nice . "<br />\n"; }