Re: Extending Exception

From: Date: Wed, 21 Jul 2010 15:25:33 +0000
Subject: Re: Extending Exception
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-307068@lists.php.net to get a copy of this message
On 21 July 2010 16:10, Christoph Boget <jcboget@hotmail.com> wrote: > Ok, so taking the sample code that is on the page > > http://us3.php.net/manual/en/language.exceptions.extending.php > > Literally, just cutting and pasting the code.  When I try to > > throw new Exception( 'My Message' ); > > PHP spits out the following fatal error: > > Fatal error: Wrong parameters for Exception([string $exception [, long $code ]]) > > and the line number it dies on is the line in the > MyException::__construct() where it is calling the parent's > constructor.  So what's going on?  Is the documentation wrong?  Is > this a bug?  Am I doing something wrong? > > I'm using PHP 5.2.9. > > thnx, > Christoph > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > As of V5.3.0, exceptions can be nested [1]. The example code will generate the following errors. V5.0.x : Fatal error: Argument 3 must not be null V5.1.0 - V5.1.1 : Fatal error: Wrong parameter count for exception([string $exception [, long $code ]]) V5.1.1 - V5.2.x : Fatal error: Wrong parameters for Exception([string $exception [, long $code ]]) The nesting is controlled by the 3rd parameter to the constructor. Removing this will allow the code to work on your version. <?php class MyException extends Exception { // Redefine the exception so message isn't optional public function __construct($message, $code = 0) { // some code // make sure everything is assigned properly parent::__construct($message, $code); } // custom string representation of object public function __toString() { return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; } public function customFunction() { echo "A custom function for this type of exception\n"; } } ?> Regards, Richard Quadling. [1] http://us3.php.net/manual/en/migration53.new-features.php

« previous php.general (#307068) next »