Re: How can i convert one object to another. Convert Exception object to MyException Object.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Mathijs wrote:
> Hello there,
> 
> I am trying to convert one object to another, something like clone.
> 
> For example.
> ---
> class MyException extends Exception {
>     // My functions etc.. etc..   
> }
> 
> function ExceptionHandler($exception) {
>     // Convert an NoN MyException to an MyException????
> }
> 
> set_exception_handler('ExceptionHandler');
> ---
> 
> Then when i do a:
> throw new Exception('My Message', 1);

lets assume you can't throw a MyException to start with then, although
it does beg the question why.

secondly if the Exception reaches ExceptionHandler what is the point of
converting it? it would seem to me that rethrowing a MyException at that
point would only result in an uncaught exception, no?

anyway here is some untest stuff, although going by the manual
it's gonig to be a partial solution (given that the methods or Exception
are final) depending on what exactly you do/want with MyException:

class MyWrapperException extends MyException
{
	private $mytrace;

	function __construct($e) {
		parent::__construct();
		// no idea if these will be set properly
		$this->message  = $e->getMessage();
		$this->code	= $e->getCode();
		$this->file	= $e->getFile();
		$this->line	= $e->getLine();

		// it doesn't seem possible to override the stack trace of
		// an exception
		$this->mytrace  = $e->getTrace();
	}
}

function ExceptionHandler($e) {
	$my = new MyWrapperException($e);
	// do something with $my
}


> 
> And i don't catch it my self, it will get into the ExceptionHandler.
> Now i have some special methods within MyException to handle stuff.
> And i want to copy/convert the $exception (Exception) object to a
> MyException object to use its methods.
> 
> Thx in advanced.
> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux