Skip to content

Commit 7df7867

Browse files
committed
Merge branch 'PHP-8.0'
* Fix the signature of PDOStatement::fetchObject()
2 parents 1779f68 + 068c8db commit 7df7867

File tree

4 files changed

+108
-11
lines changed

4 files changed

+108
-11
lines changed

ext/pdo/pdo_stmt.c

+6-8
Original file line numberDiff line numberDiff line change
@@ -845,8 +845,8 @@ static bool do_fetch(pdo_stmt_t *stmt, zval *return_value, enum pdo_fetch_type h
845845
return 0;
846846
}
847847
if (!stmt->fetch.cls.fci.size) {
848-
if (!do_fetch_class_prepare(stmt))
849-
{
848+
if (!do_fetch_class_prepare(stmt)) {
849+
zval_ptr_dtor(return_value);
850850
return 0;
851851
}
852852
}
@@ -1200,12 +1200,10 @@ PHP_METHOD(PDOStatement, fetchObject)
12001200

12011201
do_fetch_opt_finish(stmt, 0);
12021202

1203-
if (ctor_args) {
1204-
if (Z_TYPE_P(ctor_args) == IS_ARRAY && zend_hash_num_elements(Z_ARRVAL_P(ctor_args))) {
1205-
ZVAL_ARR(&stmt->fetch.cls.ctor_args, zend_array_dup(Z_ARRVAL_P(ctor_args)));
1206-
} else {
1207-
ZVAL_UNDEF(&stmt->fetch.cls.ctor_args);
1208-
}
1203+
if (ctor_args && zend_hash_num_elements(Z_ARRVAL_P(ctor_args))) {
1204+
ZVAL_ARR(&stmt->fetch.cls.ctor_args, zend_array_dup(Z_ARRVAL_P(ctor_args)));
1205+
} else {
1206+
ZVAL_UNDEF(&stmt->fetch.cls.ctor_args);
12091207
}
12101208
if (ce) {
12111209
stmt->fetch.cls.ce = ce;

ext/pdo/pdo_stmt.stub.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function fetchAll(int $mode = PDO::FETCH_DEFAULT, mixed ...$args) {}
4343
public function fetchColumn(int $column = 0) {}
4444

4545
/** @return object|false */
46-
public function fetchObject(?string $class = "stdClass", ?array $ctorArgs = null) {}
46+
public function fetchObject(?string $class = "stdClass", array $constructorArgs = []) {}
4747

4848
/** @return mixed */
4949
public function getAttribute(int $name) {}

ext/pdo/pdo_stmt_arginfo.h

+2-2
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: 85a2096daf8411ab2c35af049665b567dac9ac5d */
2+
* Stub hash: 95416f12e78609bbb9ed2e70835180654f8e78c8 */
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_WITH_DEFAULT_VALUE(0, ctorArgs, IS_ARRAY, 1, "null")
58+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, constructorArgs, IS_ARRAY, 0, "[]")
5959
ZEND_END_ARG_INFO()
6060

6161
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_PDOStatement_getAttribute, 0, 0, 1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
--TEST--
2+
MySQL PDO: PDOStatement->fetchObject() with $constructorArgs
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 int $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, []);
53+
} catch (ArgumentCountError $exception) {
54+
echo $exception->getMessage() . "\n";
55+
}
56+
57+
$stmt->execute();
58+
$obj = $stmt->fetchObject(Foo::class, ["a" => 123]);
59+
var_dump($obj);
60+
61+
$stmt->execute();
62+
$obj = $stmt->fetchObject(Bar::class);
63+
var_dump($obj);
64+
65+
$stmt->execute();
66+
$obj = $stmt->fetchObject(Bar::class, []);
67+
var_dump($obj);
68+
69+
try {
70+
$stmt->execute();
71+
$obj = $stmt->fetchObject(Bar::class, ["a" => 123]);
72+
} catch (Error $exception) {
73+
echo $exception->getMessage() . "\n";
74+
}
75+
76+
?>
77+
--CLEAN--
78+
<?php
79+
require __DIR__ . '/mysql_pdo_test.inc';
80+
MySQLPDOTest::dropTestTable();
81+
?>
82+
--EXPECTF--
83+
Too few arguments to function Foo::__construct(), 0 passed and exactly 1 expected
84+
Too few arguments to function Foo::__construct(), 0 passed and exactly 1 expected
85+
object(Foo)#%d (2) {
86+
["a"]=>
87+
int(123)
88+
["id"]=>
89+
int(1)
90+
}
91+
object(Bar)#%d (1) {
92+
["id"]=>
93+
int(1)
94+
}
95+
object(Bar)#%d (1) {
96+
["id"]=>
97+
int(1)
98+
}
99+
User-supplied statement does not accept constructor arguments

0 commit comments

Comments
 (0)