OOP PHP – Error Handling

<?php
class TestColor{
function __construct(){
if(!isset($_GET[‘color’])){
$this->viewform();
} else {
# try search color function
try {
if(!$this->searchColors($_GET[‘color’])){
$this->viewform();
} else {
$this->viewColor($_GET[‘color’]);
$this->viewForm();
}
}
# catch throw exception here
catch(Exception $e){
echo ($e->getMessage() . ‘<br />’);
echo (‘Error Code: ‘. $e->getCode().'<br />’);
echo (‘Script Name: ‘. $e->getFile().'<br />’);
echo (‘Line Number: ‘. $e->getLine().'<br />’);
$this->viewForm();
}
}
}

function searchColors($color){
# error: $color harus lebih dari 3 hurup
$this->color = $color;
if(strlen($this->color) < 3){
# throw an Exception
throw new Exception(‘Color to short, 3 chars min’);
}

if(strlen($this->color) > 6){
throw new Exception(‘Color to long, 6 chars max’);
}

return $this->color;
}

function viewForm(){
echo “<form action=’#’ method=’get’>
Color:<input type=’text’ name=’color’>
<input type=’submit’/>
</form>”;
}
function viewColor($color){
echo “<div
style=’min-height:1em;
min-width:2em;
background:#”.$color.”;
border:1px solid #000;’>
&nbsp;</div>”;
}
}

$tc = new TestColor();
?>

Published by

G3n1k

just to remember what i had known :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s