<?php
/**
* This scripts demonstates some basic operations with enum objects.
*/
require '../class/Co3/CodeGen/Enum.php';
use Co3\CodeGen\Enum as Enum;
//create a the enum Direction in the globsl namespace
Enum::create('Direction',array('Up','Down','Left','Right'));
//get an instance using a class constant
$up = Direction::get(Direction::Up);
$down = Direction::get(Direction::Down);
//get an instance using a string
$upToo = Direction::get('Up');
//$up == $upToo is true
echo '$up == $upToo is ';
echo ($up == $upToo)? 'true' : 'false';
echo '<br/>';
//$up === $upToo is true
echo '$up === $upToo is ';
echo ($up === $upToo)? 'true' : 'false';
echo '<br/>';
//$up == 'Up' is true
echo '$up == \'Up\' is ';
echo ($up == 'Up')? 'true' : 'false';//$up is castet to string
echo '<br/>';
//$up === 'Up' is false
echo '$up === \'Up\' is ';
echo ($up === 'Up')? 'true' : 'false';//type strict comparison
echo '<br/>';
//$up === $down is false
echo '$up == $down is ';
echo ($up == $down)? 'true' : 'false';
echo '<br/>';
//cast Direction to string
echo 'going ' . $up . '.<br/>';
echo "going $up and {$down}<br/>";
//type hinting and switch
function getOpposit (Direction $d){
//switch does not work with objects so $d is implicitly casted to a string
switch($d){
case 'Up':
return Direction::get('Down'); break;
case 'Down':
return Direction::get('Up'); break;
case 'Left':
return Direction::get('Right'); break;
case 'Right':
return Direction::get('Left'); break;
}
}
//cast to array
foreach(Direction::toArray() as $direction){
echo "The opposit of {$direction} is ";
echo getOpposit($direction);
echo '<br/>';
}
|