Jump to content

[SOLVED] Extending Exception to contain method name


Mchl

Recommended Posts

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?

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'];
  }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.