[nycphp-talk] PHP 5 Exception Expertise Needed
Jayesh Sheth
jayeshsh at ceruleansky.com
Thu Mar 17 19:18:41 EST 2005
Hello Joseph,
I think I know what is going wrong regarding the exception handling
issue you mentioned.
In the SvEx class, you declared ERR_TYPE as a constant (or static member):
const ERR_TYPE = "Unknown Error";
ApplicationEx extends SvEx, and even if you redeclare ERR_TYPE in ApplicationEx, it still retains its old, immutable value from SvES, which is "Unknown Error."
If you change ERR_TYPE to be a protected member (thus inheritable by subclasses), it should work.
For example:
<?php
class SvEx extends Exception {
protected $err_type = "Unknown Error";
const ERR_DESC = "There has been an error. This error has been
logged. Please try again in a few minutes.";
protected $_msg;
protected $_tpl;
protected $_error;
public function __construct( $error = null ) {
$this->_error = array(
'type' => $this->err_type,
'description' => self::ERR_DESC
);
/*
$this->_tpl = Application::Template();
$this->_tpl->assign( 'error_type', $this->_error['type'] );
$this->_tpl->assign( 'error_description', $this->_error['description'] );
*/
$this->_msg = $error;
parent::__construct($this->_msg);
}
public function Display() {
$this->_tpl->assign('admin', 1);
$this->_tpl->assign('msg', $this->_msg);
$this->_tpl->display('error.tpl');
}
}
?>
and then:
<?php
class ApplicationEx extends SvEx {
protected $err_type = "Application Error";
public function __construct( $msg = null ) {
parent::__construct($msg);
$this->_error['type'] = $this->err_type;
}
}
?>
---------------------------------------------
and then a test:
---------------------------------------------
<?php
// exceptions2.php
require 'SvEx.php';
require 'ApplicationEx.php';
try
{
$error = 'This error goes to the ApplicationEx exception handler';
throw new ApplicationEx($error);
}
catch (ApplicationEx $e)
{
echo 'Caught exception: ', $e->getMessage(), "\n";
echo "<pre>";
print_r($e);
echo "</pre>";
}
// Continue execution
echo 'Hello World';
?>
---------------------------------
It should output the following:
---------------------------------
Caught exception: This error goes to the ApplicationEx exception handler
ApplicationEx Object
(
[err_type:protected] => Application Error
[_msg:protected] => This error goes to the ApplicationEx exception handler
[_tpl:protected] =>
[_error:protected] => Array
(
[type] => Application Error
[description] => There has been an error. This error has been
logged. Please try again in a few minutes.
)
[message:protected] => This error goes to the ApplicationEx exception handler
[string:private] =>
[code:protected] => 0
[file:protected] => C:\apachefriends\xampp\htdocs\nyphp\exceptions2.php
[line:protected] => 10
[trace:private] => Array
(
)
)
Hello World
-------------------------------------------
Hope that helps,
- Jay Sheth
More information about the talk
mailing list