2
\$\begingroup\$

Got this idea from the php.net website on extending exceptions. The main thing I wanted with this was to have the exception name auto imprinted on the exception message, so I wouldn't have to write it in each message. So far its working, just wondering if this is efficient or not?

Thanks

class DataException extends Exception 
{
   protected $solved;
   protected $howSolved;

   public function __construct($message, $solved = false, $howSolved = null, 
                               $code = 0, Exception $previous = null) 
   {
      $this->solved = $solved;
      $this->howSolved = $howSolved;

      // make sure everything is assigned properly
      parent::__construct($message, $code, $previous);
   }

   public function setSolved($isSolved)
   {
      $this->solved = $isSolved;
   }

   public function setHowSolved($howSolved)
   {
      $this->howSolved = $howSolved;
   }

}

class GeoCoordinateOutOfBoundsException extends DataException 
{
   public function __construct($message, $solved = false, $howSolved = null, 
                               $code = 0, Exception $previous = null) 
   {
      $message = get_class($this) . "::" . $message;

      // make sure everything is assigned properly
      parent::__construct($message, $solved, $howSolved, $code, $previous);
   }    
}
\$\endgroup\$
1
\$\begingroup\$

I'd suggest to change arguments order in ctor for better compatibility with parent calss. public function __construct($message, $code = 0, Exception $previous = null, $solved = false, $howSolved = null) And getters for $soved and $howSolved seems to be missing.

Why not to use magic constant __CLASS__ instead of get_class($this), which can be written as get_class() in your case.

\$\endgroup\$
  • \$\begingroup\$ yeah forgot the getters for DataException and also changed the argument order you describe link, good idea I think. As for the magic methods, I know about them, used some for serialization, __sleep() and __wake() but I find some of those magic methods dont provide the verbosity I like to see with explicitly named methods.. self documenting Also I dont think adding functionality through inheritance is going against the core design priciples of OOP.. but thats another topic. \$\endgroup\$ – cs_brandt Mar 28 '12 at 23:40
  • \$\begingroup\$ I meant magic constant, not method. There are some "pre-defined" constants, like __CLASS__, __METHOD__, __FUNCTION__ (RTFM). __CLASS__ produces same results as get_class($this) (I do not want to open discussion that may lead to dark moments of namespaces, inheritance, traits - your case is simple). And get_class($this) may be reduced to get_class() for it has default argument. \$\endgroup\$ – Michael Mar 29 '12 at 4:42
2
\$\begingroup\$

Changing an inherited method signature isn't a good idea. It means how you call a method changes depending on what exact class you're calling.

Exception::__construct has only 3 arguments, but the first of which can be anything. As such you could do:

class DataException extends Exception 
{
   protected $solved;
   protected $howSolved;

   public function __construct($message = null, $code = 0, Exception $previous = null)
    {
    $solved = $howSolved = false;
    if (is_array($message))  {
        $solved = $message['solved']; // message is an array
        ...
        $message = $message['message']; // and now it's a string to be used/compatible with the parent
    }

Which would work whether $message was a string or an array - and without changing the method signature.

Why do you have setters for properties on a exception? Sounds like you're using exceptions for something they aren't designed.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.