Mchl Posted October 24, 2009 Share Posted October 24, 2009 I was wondering, if there's a way to extend Exception in such a way, that it would contain name of a method where it has been thrown. Right now I have this: class myException extends Exception { protected $method; public function __construct($message, $method, $code = 0, Exception $previous = null) { $this->method = $method; parent::__construct($message, $code, $previous); } public function getMethod() { return $this->method; } } but passing __METHOD__ as a parameter whenever exception is thrown doesn't look really good... [edit] Oh wait a second... I can get it from trace, can't I? Quote Link to comment https://forums.phpfreaks.com/topic/178832-solved-extending-exception-to-contain-method-name/ Share on other sites More sharing options...
Mchl Posted October 24, 2009 Author Share Posted October 24, 2009 Ok... so this seems to work as expected class myException extends Exception { public function __construct($message, $code = 0, Exception $previous = null) { parent::__construct($message, $code, $previous); } public function getMethod() { $trace = $this->getTrace(); //I can't use just $this->trace here... strange... return $trace[0]['class'].$trace[0]['type'].$trace[0]['function']; } } Quote Link to comment https://forums.phpfreaks.com/topic/178832-solved-extending-exception-to-contain-method-name/#findComment-943441 Share on other sites More sharing options...
Daniel0 Posted October 24, 2009 Share Posted October 24, 2009 //I can't use just $this->trace here... strange... That's because Exception::$trace is a private property. Quote Link to comment https://forums.phpfreaks.com/topic/178832-solved-extending-exception-to-contain-method-name/#findComment-943477 Share on other sites More sharing options...
Mchl Posted October 24, 2009 Author Share Posted October 24, 2009 Ok... not strange anymore Quote Link to comment https://forums.phpfreaks.com/topic/178832-solved-extending-exception-to-contain-method-name/#findComment-943493 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.