Jump to content

[SOLVED] Extending Exception to contain method name


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.