Re: Raising errors inside a constructor
| From: | Greg Beaver | Date: | Tue, 17 Aug 2004 02:37:03 +0000 |
| Subject: | Re: Raising errors inside a constructor | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-32722@lists.php.net to get a copy of this message | ||
Olivier Guilyardi wrote:
PEAR_Error != PEAR_ErrorStackhttp://pear.php.net/manual/en/core.pear.pear-errorstack.intro.phpWell, I had read the ErrorStack introduction before posting in here. I've read it again. Please explain me how ErrorStack allows me to do this : /** * Error Handling class * * @package Some_PEAR_Package */ class ErrorHandling { var $error = null; // this could be a stack, whatever function raiseError($message) {$this->error = new PEAR_Error($message);}
function hasError() {instead of:return !is_null($this->$error);} function popError() {return $this->error;} }
/** * A real life class for any purpose, say, counting berries * */ class Berry extends ErrorHandling { var $field = null; function Berry($field) {require_once 'PEAR/ErrorStack.php'; define('BERRY_ERROR_NOTMATURE', 1); define('BERRY_ERROR_NOTAFIELD', 2); define('BERRY_ERROR_TOOTINY', 1); class Berry {if (!$field->isMature()) { $this->raiseError("Sorry, this field's berries are too young"); } $this->field = $field;} function count() {if (!$field) { $this->raiseError("Please, specify a berry field"); return false; } // We all should have a good database in our berry fields ;-) $res = $this->field->db->query("SELECT count(*) FROM berry_stat"); list($num) = $res->fetchRow(); return $num;} }
var $field = null;
function Berry($field)
{
if (!is_a($field, 'Field')) {
PEAR_ErrorStack::staticPush('berry', BERRY_ERROR_NOTAFIELD, 'error', array('field' => $field), 'parameter is not a field!');
return;
}
// add a hypothetical
if ($field->size() < 25) {
PEAR_ErrorStack::staticPush('berry', BERRY_ERROR_TOOTINY, 'warning', array('size' => $field->size), 'Field size "%size%" is too tiny to support lindenberries');
}
if (!$field->isMature()) {
PEAR_ErrorStack::staticPush('berry', BERRY_ERROR_NOTMATURE, 'error', array(), 'Sorry, this field\'s berries are too young');
return;
}
}
etc...
and then instead of:
// // Let's count berries ! // echo "Berry counter v0.45.12\n"; $berry = new Berry($field); if ($berry->hasError()) { $e = $berry->popError(); exit($e->getMessage); } // strict type checking since we could get a 0 result if (($c = $berry->count()) !== false) { echo "There's $c berries. It's time to go and pick them !\n"; } else { $e = $berry->popError(); exit($e->getMessage); }echo "Berry counter v0.45.12\n"; $berry = new Berry($field); if (PEAR_ErrorStack::staticHasErrors('berry')) {
$e = PEAR_ErrorStack::staticGetErrors();
foreach ($e['berry'] as $error) {
echo $e['message'] . "\n";
}
exit;
}
if (($c = $berry->count()) !== false) {
echo "There's $c berries. It's time to go and pick them !\n";} else {
$e = PEAR_ErrorStack::staticGetErrors();
foreach ($e['berry'] as $error) {
echo $e['message'] . "\n";
}
exit;
}
As you can see, it's the same number of lines of code, and would actually be slightly faster than the code you've written, with the added flexibility that you can raise multiple errors in the constructor (as in my example) and also have support for warnings and so on.
Greg