NYCPHP Meetup

NYPHP.org

[nycphp-talk] NEW PHundamentals Question - Error Handling

Jayesh Sheth jayeshsh at ceruleansky.com
Tue May 11 23:12:07 EDT 2004


Hi all,

Here is what I did recently for error handling within a (PHP 4) class. 
(This was for a redesign of a class that was originally written out of 
the need to collect 10 - 20 separate functions, all having resided in 
different files.)

By the name, the naming scheme I use for class names and methods 
(functions) is unconventional - all lower case.

This class's functionality has been fictionalized for your (and my) 
enjoyment.

I am not sure if the way I do it is good or bad, but you can definitely 
let me know what you think.

class appreciate_life

{
var $bitterness; // on a scale of 0 to 10
var $sweetness; // on a scale of 0 to 10
var $satisfaction; // how satisfied you are

var $harddrivehealth; // on a scale of 0 to 10
var $freshairintake;  // on a scale of 0 to 10

var $oopsies = array(); // errors
var $oopsies4devs = array(); // debug messages

// constructor
function  appreciate_life($bitterness, $sweetness, $harddrivehealth, 
$freshairintake)
{
$this->bitterness = $bitterness;
$this->sweetness = $sweetness;
$this->harddrivehealth = $harddrivehealth;
$this->freshairintake = $freshairintake;
}

function measure_life_satisfaction()
{
	if ($harddrivehealth < 0 || $freshairintake < 0)
	{
		$this->oopsies['warning_get_out_more'] = "Your hard 		drive just went 
kaput or you have been staring at that computer screen too long. Forget 
the computer and go to the Japanese pond at the Brooklyn Botanic Garden.";
		$this->oopsies4devs['take_user_on_walk'] = "This user needs to be 
taken on a walk.";
	}	

	else if ( $this->sweetness > $this->bitterness )
	{
		return "<p>Bravo! You are one happy (wo)man! </p>";
	}

	else
	{
                 return "<p>Life sure sucks, huh? If life is a lemon, 
maybe you can make some lemonade from it ... </p>";
	}
}

function is_okay()
{
	if ( count($this->oopsies) > 0 || count($this->oopsies4devs) > 0)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function print_error_messages()
{
	/* retrieve contents of $this->oopsies array and format them for display */
}

function print_debug_messages()
{
/* retrieve contents of $this->oopsies4devs array and format them for 
display */
}

}

/*
Instantiation
*/
$debug = 0;

$life = new appreciate_life(5, 6, -1, -10);

$sat_level = $life->measure_life_satisfaction();
echo $sat_level;

if ( ! $life->is_okay() )
{
	if ( ! empty($debug) )
	{
		$life->print_debug_messages();
	}

	$life->print_error_messages();
}

/*
End
*/

I don't know if that made much sense at all - but I am way past my 
bedtime, so that is why I am a bit  ... delirious.

- Jay




More information about the talk mailing list