|
| 1 | +--TEST-- |
| 2 | +PDO Common: PDOStatement->fetchObject() with $constructorArgs when default CTORs have been set-up |
| 3 | +--EXTENSIONS-- |
| 4 | +pdo |
| 5 | +--SKIPIF-- |
| 6 | +<?php |
| 7 | +$dir = getenv('REDIR_TEST_DIR'); |
| 8 | +if (false == $dir) die('skip no driver'); |
| 9 | +require_once $dir . 'pdo_test.inc'; |
| 10 | +PDOTest::skip(); |
| 11 | +?> |
| 12 | +--FILE-- |
| 13 | +<?php |
| 14 | +if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.__DIR__ . '/../../pdo/tests/'); |
| 15 | +require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc'; |
| 16 | +$db = PDOTest::factory(); |
| 17 | + |
| 18 | +class Foo { |
| 19 | + public int $id; |
| 20 | + |
| 21 | + public function __construct(public string $v) { } |
| 22 | +} |
| 23 | +class Bar { |
| 24 | + public int $id; |
| 25 | +} |
| 26 | + |
| 27 | +$table = 'pdo_stmt_fetchobject_ctor_args_after_default'; |
| 28 | +$db->exec("CREATE TABLE {$table} (id INT, label CHAR(1), PRIMARY KEY(id))"); |
| 29 | +$db->exec("INSERT INTO {$table} (id, label) VALUES (1, 'a')"); |
| 30 | +$db->exec("INSERT INTO {$table} (id, label) VALUES (2, 'b')"); |
| 31 | +$db->exec("INSERT INTO {$table} (id, label) VALUES (3, 'c')"); |
| 32 | + |
| 33 | +$query = "SELECT id FROM {$table} ORDER BY id ASC"; |
| 34 | +$stmt = $db->prepare($query); |
| 35 | +$stmt->setFetchMode(PDO::FETCH_CLASS, Foo::class, ['Hello']); |
| 36 | +$stmt->execute(); |
| 37 | + |
| 38 | +var_dump($stmt->fetch()); |
| 39 | +try { |
| 40 | + $obj = $stmt->fetchObject(Bar::class, ['no-args']); |
| 41 | + var_dump($obj); |
| 42 | +} catch (Throwable $e) { |
| 43 | + echo $e::class, ': ', $e->getMessage(), "\n"; |
| 44 | +} |
| 45 | +var_dump($stmt->fetch()); |
| 46 | + |
| 47 | + |
| 48 | +?> |
| 49 | +--CLEAN-- |
| 50 | +<?php |
| 51 | +require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc'; |
| 52 | +$db = PDOTest::factory(); |
| 53 | +PDOTest::dropTableIfExists($db, "pdo_stmt_fetchobject_ctor_args_after_default"); |
| 54 | +?> |
| 55 | +--EXPECTF-- |
| 56 | +object(Foo)#3 (2) { |
| 57 | + ["id"]=> |
| 58 | + int(1) |
| 59 | + ["v"]=> |
| 60 | + string(5) "Hello" |
| 61 | +} |
| 62 | +ValueError: PDOStatement::fetchObject(): Argument #2 ($constructorArgs) must be empty when class provided in argument #1 ($class) does not have a constructor |
| 63 | + |
| 64 | +Fatal error: Uncaught ArgumentCountError: Too few arguments to function Foo::__construct(), 0 passed and exactly 1 expected in %s:%d |
| 65 | +Stack trace: |
| 66 | +#0 [internal function]: Foo->__construct() |
| 67 | +#1 %s(%d): PDOStatement->fetch() |
| 68 | +#2 {main} |
| 69 | + thrown in %s on line %d |
0 commit comments