Skip to content

Commit b77f5c3

Browse files
committed
Fix ZPP for $ctorArgs, and add test
1 parent 83ead55 commit b77f5c3

File tree

4 files changed

+123
-10
lines changed

4 files changed

+123
-10
lines changed

ext/pdo/pdo_stmt.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,7 @@ PHP_METHOD(PDOStatement, fetchObject)
12541254
ZEND_PARSE_PARAMETERS_START(0, 2)
12551255
Z_PARAM_OPTIONAL
12561256
Z_PARAM_CLASS_OR_NULL(ce)
1257-
Z_PARAM_ARRAY(ctor_args)
1257+
Z_PARAM_ARRAY_OR_NULL(ctor_args)
12581258
ZEND_PARSE_PARAMETERS_END();
12591259

12601260
PHP_STMT_GET_OBJ;
@@ -1266,12 +1266,10 @@ PHP_METHOD(PDOStatement, fetchObject)
12661266

12671267
do_fetch_opt_finish(stmt, 0);
12681268

1269-
if (ctor_args) {
1270-
if (Z_TYPE_P(ctor_args) == IS_ARRAY && zend_hash_num_elements(Z_ARRVAL_P(ctor_args))) {
1271-
ZVAL_ARR(&stmt->fetch.cls.ctor_args, zend_array_dup(Z_ARRVAL_P(ctor_args)));
1272-
} else {
1273-
ZVAL_UNDEF(&stmt->fetch.cls.ctor_args);
1274-
}
1269+
if (ctor_args && zend_hash_num_elements(Z_ARRVAL_P(ctor_args))) {
1270+
ZVAL_ARR(&stmt->fetch.cls.ctor_args, zend_array_dup(Z_ARRVAL_P(ctor_args)));
1271+
} else {
1272+
ZVAL_UNDEF(&stmt->fetch.cls.ctor_args);
12751273
}
12761274
if (ce) {
12771275
stmt->fetch.cls.ce = ce;

ext/pdo/pdo_stmt.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function fetchAll(int $mode = PDO::FETCH_DEFAULT, mixed ...$args) {}
4141
public function fetchColumn(int $column = 0) {}
4242

4343
/** @return object|false */
44-
public function fetchObject(?string $class = "stdClass", array $ctorArgs = UNKNOWN) {}
44+
public function fetchObject(?string $class = "stdClass", ?array $ctorArgs = null) {}
4545

4646
/** @return mixed */
4747
public function getAttribute(int $name) {}

ext/pdo/pdo_stmt_arginfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 9b654220433724fc075d2f1afd01ae4c05a70731 */
2+
* Stub hash: 80860ee99befe1258900120f0c226688f6606c6f */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_PDOStatement_bindColumn, 0, 0, 2)
55
ZEND_ARG_TYPE_MASK(0, column, MAY_BE_STRING|MAY_BE_LONG, NULL)
@@ -55,7 +55,7 @@ ZEND_END_ARG_INFO()
5555

5656
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_PDOStatement_fetchObject, 0, 0, 0)
5757
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, class, IS_STRING, 1, "\"stdClass\"")
58-
ZEND_ARG_TYPE_INFO(0, ctorArgs, IS_ARRAY, 0)
58+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, ctorArgs, IS_ARRAY, 1, "null")
5959
ZEND_END_ARG_INFO()
6060

6161
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_PDOStatement_getAttribute, 0, 0, 1)
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
--TEST--
2+
MySQL PDO: PDOStatement->fetchObject() with $ctorArgs
3+
--SKIPIF--
4+
<?php
5+
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'skipif.inc');
6+
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
7+
MySQLPDOTest::skip();
8+
$db = MySQLPDOTest::factory();
9+
10+
try {
11+
$query = "SELECT '', NULL, \"\" FROM DUAL";
12+
$stmt = $db->prepare($query);
13+
$ok = @$stmt->execute();
14+
} catch (PDOException $e) {
15+
die("skip: Test cannot be run with SQL mode ANSI");
16+
}
17+
if (!$ok)
18+
die("skip: Test cannot be run with SQL mode ANSI");
19+
?>
20+
--FILE--
21+
<?php
22+
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
23+
/** @var PDO $db */
24+
$db = MySQLPDOTest::factory();
25+
MySQLPDOTest::createTestTable($db);
26+
27+
$query = "SELECT id FROM test ORDER BY id ASC LIMIT 1";
28+
$stmt = $db->prepare($query);
29+
30+
class Foo {
31+
public int $a;
32+
public int $id;
33+
34+
public function __construct($a) {
35+
$this->a = $a;
36+
}
37+
}
38+
39+
class Bar {
40+
public $id;
41+
}
42+
43+
$stmt->execute();
44+
try {
45+
$obj = $stmt->fetchObject(Foo::class);
46+
} catch (ArgumentCountError $exception) {
47+
echo $exception->getMessage() . "\n";
48+
}
49+
50+
$stmt->execute();
51+
try {
52+
$obj = $stmt->fetchObject(Foo::class, null);
53+
} catch (ArgumentCountError $exception) {
54+
echo $exception->getMessage() . "\n";
55+
}
56+
57+
$stmt->execute();
58+
try {
59+
$obj = $stmt->fetchObject(Foo::class, []);
60+
} catch (ArgumentCountError $exception) {
61+
echo $exception->getMessage() . "\n";
62+
}
63+
64+
$stmt->execute();
65+
$obj = $stmt->fetchObject(Foo::class, ["a" => 123]);
66+
var_dump($obj);
67+
68+
$stmt->execute();
69+
$obj = $stmt->fetchObject(Bar::class);
70+
var_dump($obj);
71+
72+
$stmt->execute();
73+
$obj = $stmt->fetchObject(Bar::class, null);
74+
var_dump($obj);
75+
76+
$stmt->execute();
77+
$obj = $stmt->fetchObject(Bar::class, []);
78+
var_dump($obj);
79+
80+
try {
81+
$stmt->execute();
82+
$obj = $stmt->fetchObject(Bar::class, ["a" => 123]);
83+
} catch (Error $exception) {
84+
echo $exception->getMessage() . "\n";
85+
}
86+
87+
?>
88+
--CLEAN--
89+
<?php
90+
require __DIR__ . '/mysql_pdo_test.inc';
91+
MySQLPDOTest::dropTestTable();
92+
?>
93+
--EXPECTF--
94+
Too few arguments to function Foo::__construct(), 0 passed and exactly 1 expected
95+
Too few arguments to function Foo::__construct(), 0 passed and exactly 1 expected
96+
Too few arguments to function Foo::__construct(), 0 passed and exactly 1 expected
97+
object(Foo)#%d (2) {
98+
["a"]=>
99+
int(123)
100+
["id"]=>
101+
int(1)
102+
}
103+
object(Bar)#%d (1) {
104+
["id"]=>
105+
int(1)
106+
}
107+
object(Bar)#%d (1) {
108+
["id"]=>
109+
int(1)
110+
}
111+
object(Bar)#%d (1) {
112+
["id"]=>
113+
int(1)
114+
}
115+
User-supplied statement does not accept constructor arguments

0 commit comments

Comments
 (0)