Re: Raising errors inside a constructor

From: Date: Mon, 16 Aug 2004 21:44:41 +0000
Subject: Re: Raising errors inside a constructor
References: 1 2 3 4 5 6  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-32712@lists.php.net to get a copy of this message
Hans L wrote:
Ok, so what you have proposed is simply "why not use PEAR_ErrorStack for error handling". Of course you can do this! -- this is an official PEAR error handling option. It sounds like you haven't seen these docs: http://pear.php.net/manual/en/core.pear.pear-errorstack.intro.php
Well, 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() {
      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) {
      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;
} } // // 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); } -- og

« previous php.pear.dev (#32712) next »