[php-src] Issue #12480: Cannot use PDO with readonly and constructor property promotion
| From: | GregoireHebert | Date: | Thu, 19 Oct 2023 16:24:56 +0000 |
| Subject: | [php-src] Issue #12480: Cannot use PDO with readonly and constructor property promotion | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-245607@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/12480
Author: GregoireHebert
### Description
The following code:
```php
<?php
class Foo
{
public function __construct(public readonly int $prop){}
}
$pdo = new \PDO('sqlite:'.__DIR__.'/db');
$pdo->exec('CREATE TABLE IF NOT EXISTS Foo (prop integer not null primary key)');
$pdo->exec('REPLACE INTO Foo values (1)');
$preparation = $pdo->prepare(
'SELECT * FROM Foo WHERE prop = 1'
);
$preparation->setFetchMode(\PDO::FETCH_CLASS, Foo::class);
$preparation->execute();
$result = $preparation->fetch();
var_dump($result);
```
Resulted in this output:
```
PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function Foo::__construct(), 0
passed and exactly 1 expected in /Users/gheb/test.php:5
Stack trace:
#0 [internal function]: Foo->__construct()
#1 /Users/gheb//test.php(18): PDOStatement->fetch()
#2 {main}
thrown in /Users/gheb//test.php on line 5
```
But I expected this output instead:
```
object(Foo)#1 (1) {
["prop"]=>
int(1)
}
```
Note that if default values were set, you would get (as expected):
```
Fatal error: Uncaught Error: Cannot modify readonly property Foo::$prop
```
### PHP Version
php >=8.1
### Operating System
_No response_