Re: PEAR_Warning proof of concept
| From: | Alan Knowles | Date: | Sat, 10 Jul 2004 08:22:15 +0000 |
| Subject: | Re: PEAR_Warning proof of concept | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-31822@lists.php.net to get a copy of this message | ||
Following the exception extends thread:
I'm not sure you need errortypes or codes - if there is a really a need to diferenciate errors, then doing custom package warning classes, with the same extension trick that exceptions do, may prove more effective.
I'm not sure you need the $myclass in the warning -
require_once 'DB/Warning.php';
new DB_Warning("some warning");
is alot clearer than
require_once 'DB/Warning.php';
require_once 'PEAR/Warning.php';
PEAR_Warning::newWarning($msg,null,null,1, 'DB_Warning');
Regards
Alan
Greg Beaver wrote:
------------------------------------------------------------------------ <?php require_once 'PEAR/Exception.php'; class PEAR_Warning extends PEAR_Exception { static $errormask = 3; const ERROR = 1; const WARNING = 2; const NOTICE = 4; const DEBUG = 8; protected $level = 2; protected $multipleWarnings = array(); static public function newWarning($msg, $p1 = null, $p2 = null, $level = self::WARNING, $myclass = 'PEAR_Warning') {if (self::$errormask & $level) { return new $myclass($msg, $p1, $p2); } else { return false; }} public function addWarning($msg, $pl = null, $p2 = null, $level = self::WARNING) {if (self::$errormask & $level) { $myclass = get_class($this); return new $myclass($msg, $p1, $p2); } else { return false; }} public function __toString() {$str = get_class($this) . " occurred: \n" . " Error message: {$this->message}\n" . " Error code : {$this->code}\n" . " File (Line) : {$this->file} ({$this->line})\n" . " Method : {$this->_method}\n"; if ($this->cause instanceof Exception) { $str .= " Nested Error :\n" . $this->_getCauseMessage(); } if (count($this->multipleWarnings)) { foreach ($this->multipleWarnings as $warn) { $str .= $warn->__toString(); } } if (isset($_SERVER['REQUEST_URI'])) { return nl2br('<pre>'.htmlspecialchars($str).'</pre>'); } return $str;} } function pear_warning($msg, $p1 = null, $p2 = null, $level = PEAR_Warning::WARNING) { return PEAR_Warning::newWarning($msg, $p1, $p2, $level); } ?>