[php-src] Issue #14673: Cannot set exception code for custom exception
| From: | Chi-teck | Date: | Wed, 26 Jun 2024 13:44:26 +0000 |
| Subject: | [php-src] Issue #14673: Cannot set exception code for custom exception | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-247154@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/14673
Author: Chi-teck
### Description
The following code:
```php
<?php
class Example extends \Exception {
protected $code = 5;
}
echo ' → ', (new Example(''))->getCode(), \PHP_EOL;
echo '0 → ', (new Example('', 0))->getCode(), \PHP_EOL;
echo '1 → ', (new Example('', 1))->getCode(), \PHP_EOL;
```
Resulted in this output:
```
→ 5
0 → 5
1 → 1
```
But I expected this output instead:
```
→ 5
0 → 0
1 → 1
```
There is [a note](https://www.php.net/manual/en/exception.construct.php) about default parameters in
exception constructor on php.net. However, case 2 in the above excaple explicitly pass the code
parameter to the constructor.
```
Calling the constructor of class Exception from a subclass ignores the default arguments, if the
properties $code and $message are already set.) about default parameters in exception constructor on
php.net.
### PHP Version
PHP 8.2
### Operating System
Debian 11