Copy Code code as follows:
<?php
/**
* Interpreter Example
*
* @create_date: 2010-01-04
*/
Class Expression
{
function Interpreter ($STR)
{
return $str;
}
}
Class Expressionnum extends Expression
{
function Interpreter ($STR)
{
Switch ($STR)
{
Case "0": Return "0";
Case "1": Return "one";
Case "2": Return "two";
Case "3": Return "three";
Case "4": Return "four";
Case "5": Return "five";
Case "6": Return "six";
Case "7": Return "seven";
Case "8": Return "eight";
Case "9": Return "nine";
}
}
}
Class Expressioncharater extends Expression
{
function Interpreter ($STR)
{
Return Strtoupper ($STR);
}
}
Class interpreter
{
function Execute ($string)
{
$expression = null;
for ($i = 0; $i <strlen ($string); $i + +) {
$temp = $string [$i];
Switch (TRUE)
{
Case Is_numeric ($temp): $expression = new Expressionnum (); Break
Default: $expression = new Expressioncharater ();
}
echo $expression->interpreter ($temp);
}
}
}
$obj = new Interpreter ();
$obj->execute ("12345abc");
?>