Skip to content

Commit 670036e

Browse files
committed
Use Z_PARAM_CLASS in PDOStatement::fetchObject()
Instead of implementing custom logic.
1 parent 1511bda commit 670036e

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

Zend/zend_API.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,9 @@ ZEND_API ZEND_COLD void ZEND_FASTCALL zend_argument_value_error(uint32_t arg_num
14051405
#define Z_PARAM_CLASS(dest) \
14061406
Z_PARAM_CLASS_EX(dest, 0, 0)
14071407

1408+
#define Z_PARAM_CLASS_OR_NULL(dest) \
1409+
Z_PARAM_CLASS_EX(dest, 1, 0)
1410+
14081411
#define Z_PARAM_CLASS_NAME_OR_OBJ_EX(dest, allow_null) \
14091412
Z_PARAM_PROLOGUE(0, 0); \
14101413
if (UNEXPECTED(!zend_parse_arg_class_name_or_obj(_arg, &dest, allow_null))) { \

ext/pdo/pdo_stmt.c

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,14 +1191,14 @@ PHP_METHOD(PDOStatement, fetchObject)
11911191
zend_long how = PDO_FETCH_CLASS;
11921192
zend_long ori = PDO_FETCH_ORI_NEXT;
11931193
zend_long off = 0;
1194-
zend_string *class_name = NULL;
1194+
zend_class_entry *ce = NULL;
11951195
zend_class_entry *old_ce;
11961196
zval old_ctor_args, *ctor_args = NULL;
1197-
int error = 0, old_arg_count;
1197+
int old_arg_count;
11981198

11991199
ZEND_PARSE_PARAMETERS_START(0, 2)
12001200
Z_PARAM_OPTIONAL
1201-
Z_PARAM_STR_EX(class_name, 1, 0)
1201+
Z_PARAM_CLASS_OR_NULL(ce)
12021202
Z_PARAM_ARRAY(ctor_args)
12031203
ZEND_PARSE_PARAMETERS_END();
12041204

@@ -1222,31 +1222,21 @@ PHP_METHOD(PDOStatement, fetchObject)
12221222
ZVAL_UNDEF(&stmt->fetch.cls.ctor_args);
12231223
}
12241224
}
1225-
if (class_name && !error) {
1226-
stmt->fetch.cls.ce = zend_fetch_class(class_name, ZEND_FETCH_CLASS_AUTO);
1227-
1228-
if (!stmt->fetch.cls.ce) {
1229-
pdo_raise_impl_error(stmt->dbh, stmt, "HY000", "Could not find user-supplied class");
1230-
error = 1;
1231-
}
1232-
} else if (!error) {
1225+
if (ce) {
1226+
stmt->fetch.cls.ce = ce;
1227+
} else {
12331228
stmt->fetch.cls.ce = zend_standard_class_def;
12341229
}
12351230

1236-
if (!error && !do_fetch(stmt, TRUE, return_value, how, ori, off, 0)) {
1237-
error = 1;
1238-
}
1239-
if (error) {
1231+
if (!do_fetch(stmt, TRUE, return_value, how, ori, off, 0)) {
12401232
PDO_HANDLE_STMT_ERR();
1233+
RETVAL_FALSE;
12411234
}
12421235
do_fetch_opt_finish(stmt, 1);
12431236

12441237
stmt->fetch.cls.ce = old_ce;
12451238
ZVAL_COPY_VALUE(&stmt->fetch.cls.ctor_args, &old_ctor_args);
12461239
stmt->fetch.cls.fci.param_count = old_arg_count;
1247-
if (error) {
1248-
RETURN_FALSE;
1249-
}
12501240
}
12511241
/* }}} */
12521242

ext/pdo_mysql/tests/pdo_mysql_stmt_fetchobject.phpt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ try {
6767

6868
var_dump($rows[$rowno - 1]);
6969

70+
try {
71+
$stmt->fetchObject('class_does_not_exist');
72+
} catch (TypeError $e) {
73+
echo $e->getMessage(), "\n";
74+
}
7075
} catch (PDOException $e) {
7176
// we should never get here, we use warnings, but never trust a system...
7277
printf("[001] %s, [%s} %s\n",
@@ -106,4 +111,5 @@ object(myclass)#%d (4) {
106111
["null"]=>
107112
NULL
108113
}
114+
PDOStatement::fetchObject(): Argument #1 ($class_name) must be a valid class name, class_does_not_exist given
109115
done!

0 commit comments

Comments
 (0)