Question: PHP 7: Examine the code from this section. Are there any areas in which error checking could have been converted to exception handling? Go to
PHP 7:
Examine the code from this section. Are there any areas in which error checking could have been converted to exception handling? Go to the books web site and download the code for this section. Make the potential changes to the existing code to use additional exception handling.
Example 5-6. The dog_interface.php file with exception handling (downloaded from book website)
function clean_input($value)
{
$value = htmlentities($value);
// Removes any html from the string and turns it into < format
$value = strip_tags($value);
if (get_magic_quotes_gpc())
{
$value = stripslashes($value);
// Gets rid of unwanted slashes
}
$value = htmlentities($value);
// Removes any html from the string and turns it into < format
$bad_chars = array( "{", "}", "(", ")", ";", ":", "<", ">", "/", "$" );
$value = str_ireplace($bad_chars,"",$value);
return $value;
}
class setException extends Exception {
public function errorMessage() {
list($name_error, $breed_error, $color_error, $weight_error) = explode(',', $this->getMessage());
$name_error == 'TRUE' ? $eMessage = '' : $eMessage = 'Name update not successful
';
$breed_error == 'TRUE' ? $eMessage .= '' : $eMessage .= 'Breed update not successful
';
$color_error == 'TRUE' ? $eMessage .= '' : $eMessage .= 'Color update not successful
';
$weight_error == 'TRUE' ? $eMessage .= '' : $eMessage .= 'Weight update not successful
';
return $eMessage;
}
}
function get_dog_app_properties($lab)
{
print "Your dog's name is " . $lab->get_dog_name() . "
";
print "Your dog weights " . $lab->get_dog_weight() . " lbs.
";
print "Your dog's breed is " . $lab->get_dog_breed() . "
";
print "Your dog's color is " . $lab->get_dog_color() . "
";
}
//----------------Main Section-------------------------------------
try {
if ( file_exists("e55dog_container.php"))
{
Require_once("e55dog_container.php");
}
else
{
throw new Exception("Dog container file missing or corrupt");
}
if (isset($_POST['dog_app']))
{
if ((isset($_POST['dog_name'])) && (isset($_POST['dog_breed'])) && (isset($_POST['dog_color'])) && (isset($_POST['dog_weight'])))
{
$container = new dog_container(clean_input($_POST['dog_app']));
$dog_name = clean_input(filter_input(INPUT_POST, "dog_name"));
$dog_breed = clean_input($_POST['dog_breed']);
$dog_color = clean_input($_POST['dog_color']);
$dog_weight = clean_input($_POST['dog_weight']);
$breedxml = $container->get_dog_application("breeds");
$properties_array = array($dog_name,$dog_breed,$dog_color,$dog_weight,$breedxml);
$lab = $container->create_object($properties_array);
$container = NULL;
print "Updates successful
";
get_dog_app_properties($lab);
}
else
{
print "
Missing or invalid parameters. Please go back to the dog.html page to enter valid information.
";
print "Dog Creation Page";
}
}
else // select box
{
$container = new dog_container("selectbox");
$properties_array = array("selectbox");
$lab = $container->create_object($properties_array);
$container->set_app("breeds");
$dog_app = $container->get_dog_application("breeds");
$method_array = get_class_methods($lab);
$last_position = count($method_array) - 1;
$method_name = $method_array[$last_position];
$result = $lab->$method_name($dog_app);
print $result;
}
}
catch(setException $e)
{
echo $e->errorMessage();
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
