From: Christoph Boget on
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
From: Ashley Sheridan on
On Wed, 2010-07-21 at 11:10 -0400, Christoph Boget 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
>


Well, it's not really a copy and paste job, as you've omitted the second
argument to the Exception method, which is what the error is complaining
about.

Thanks,
Ash
http://www.ashleysheridan.co.uk


From: Richard Quadling on
On 21 July 2010 16:10, Christoph Boget <jcboget(a)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
From: Richard Quadling on
On 21 July 2010 16:14, Ashley Sheridan <ash(a)ashleysheridan.co.uk> wrote:
> On Wed, 2010-07-21 at 11:10 -0400, Christoph Boget 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
>>
>
>
> Well, it's not really a copy and paste job, as you've omitted the second
> argument to the Exception method, which is what the error is complaining
> about.
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>

Ashley, the second argument is optional, hence the [] around the [, long $code].

The issue is one of BC as I've described above. Removing the BC allows
the code to work as documented.
From: Richard Quadling on
On 21 July 2010 16:30, Christoph Boget <jcboget(a)hotmail.com> wrote:
>> [1] http://us3.php.net/manual/en/migration53.new-features.php
>
> Thanks for this.  And thanks for your explanation.  I think it would have
> been nice for there to have been some mention anywhere near/around example
> #2 on http://us3.php.net/manual/en/language.exceptions.extending.php to have
> mentioned it would only work on 5.3+.  :/
> Again, thanks.
> thnx,
> Christoph

http://svn.php.net/viewvc?view=revision&revision=301446