[php-src] master: ext/pdo: build FETCH_OBJ rows as a properties table (#25516)

From: Date: Tue, 24 Nov 2026 19:07:45 +0000
Subject: [php-src] master: ext/pdo: build FETCH_OBJ rows as a properties table (#25516)
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-144128@lists.php.net to get a copy of this message
Author: Artem Ukrainskiy (ArtUkrainskiy) Committer: GitHub (web-flow) Pusher: kamil-tekiela Date: 2026-11-24T19:07:41Z Commit: https://github.com/php/php-src/commit/680e27a445f992ab0ca1a78183a3729089f39366 Raw diff: https://github.com/php/php-src/commit/680e27a445f992ab0ca1a78183a3729089f39366.diff ext/pdo: build FETCH_OBJ rows as a properties table (#25516) do_fetch() wrote each column of a FETCH_OBJ row into an empty stdClass with zend_update_property_ex(), which goes through the generic write_property handler and grows the properties table from 8 slots. The row is now collected into a hash table sized for the column count with zend_hash_update(), and the stdClass is created around it with object_and_properties_init(). Column names are stored as string keys, as the property handler stores them, and a repeated name replaces the earlier value in place. A column name that starts with "\0" now gives a stdClass with the same properties as (object) of the FETCH_ASSOC row instead of an Error. SQLite, PostgreSQL and dblib can't return such a name, and MySQL 8.4 didn't in my tests. query() + fetchAll(PDO::FETCH_OBJ) of 100 rows from SQLite and MySQL 8.4, AMD Zen 3, GCC 15.2 and clang 21.1: 12-20% fewer cycles with 10 or 30 columns, 1,378-1,405 fewer instructions per row with 10 columns without FETCH_GROUP. Same output as master. Changed paths: A ext/pdo_sqlite/tests/pdo_sqlite_fetch_obj_column_names.phpt M UPGRADING M ext/pdo/pdo_stmt.c Diff: diff --git a/UPGRADING b/UPGRADING index 9bb828a342ee..607f9427ed6b 100644 --- a/UPGRADING +++ b/UPGRADING @@ -224,6 +224,9 @@ PHP 8.7 UPGRADE NOTES . Parsing HTML with Dom\HTMLDocument or innerHTML uses much less memory for small inputs. +- PDO: + . Improved performance of fetching rows with PDO::FETCH_OBJ. + - Standard: . Improved performance of strcspn() and strpbrk() when searching longer strings with a single-byte character mask. diff --git a/ext/pdo/pdo_stmt.c b/ext/pdo/pdo_stmt.c index bfdba4ed056f..87b47d660238 100644 --- a/ext/pdo/pdo_stmt.c +++ b/ext/pdo/pdo_stmt.c @@ -747,7 +747,9 @@ static bool do_fetch(pdo_stmt_t *stmt, zval *return_value, enum pdo_fetch_type h case PDO_FETCH_OBJ: ce = zend_standard_class_def; - object_init(return_value); + /* Collect the row into the properties table, the object is created after the loop. + * With a group key the first column is the key, not a property. */ + array_init_size(return_value, stmt->column_count - (group_key != NULL)); break; case PDO_FETCH_CLASS: @@ -912,8 +914,11 @@ static bool do_fetch(pdo_stmt_t *stmt, zval *return_value, enum pdo_fetch_type h zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &val); break; - case PDO_FETCH_CLASS: case PDO_FETCH_OBJ: + zend_hash_update(Z_ARRVAL_P(return_value), column_name, &val); + break; + + case PDO_FETCH_CLASS: case PDO_FETCH_INTO: zend_update_property_ex(ce, Z_OBJ_P(return_value), column_name, &val); zval_ptr_dtor(&val); @@ -926,6 +931,10 @@ static bool do_fetch(pdo_stmt_t *stmt, zval *return_value, enum pdo_fetch_type h } } + if (how == PDO_FETCH_OBJ) { + object_and_properties_init(return_value, zend_standard_class_def, Z_ARRVAL_P(return_value)); + } + /* Run constructor for objects if not already run and not unserialized */ if (how == PDO_FETCH_CLASS && ce->constructor && !(flags & (PDO_FETCH_PROPS_LATE | PDO_FETCH_SERIALIZE))) { bool failed = pdo_call_fetch_object_constructor(ce->constructor, ctor_arguments, Z_OBJ_P(return_value)); diff --git a/ext/pdo_sqlite/tests/pdo_sqlite_fetch_obj_column_names.phpt b/ext/pdo_sqlite/tests/pdo_sqlite_fetch_obj_column_names.phpt new file mode 100644 index 000000000000..8ba23f7e09f1 --- /dev/null +++ b/ext/pdo_sqlite/tests/pdo_sqlite_fetch_obj_column_names.phpt @@ -0,0 +1,73 @@ +--TEST-- +PDO_sqlite: PDO::FETCH_OBJ with duplicate, numeric and empty column names +--EXTENSIONS-- +pdo_sqlite +--FILE-- +<?php +$db = new PDO('sqlite::memory:'); +$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + +$row = $db->query('SELECT 1 AS a, 2 AS a, 3 AS "0", 4 AS "01", 5 AS "", 6 AS "-1"')->fetch(PDO::FETCH_OBJ); +var_dump($row); +var_dump($row->{'0'}, (array) $row); + +$row->a = 'changed'; +$row->extra = true; +unset($row->{'01'}); +var_dump($row); + +var_dump($db->query('SELECT 1 AS g, 2 AS v UNION ALL SELECT 1, 3')->fetchAll(PDO::FETCH_OBJ | PDO::FETCH_GROUP)); +?> +--EXPECTF-- +object(stdClass)#%d (5) { + ["a"]=> + int(2) + ["0"]=> + int(3) + ["01"]=> + int(4) + [""]=> + int(5) + ["-1"]=> + int(6) +} +int(3) +array(5) { + ["a"]=> + int(2) + [0]=> + int(3) + ["01"]=> + int(4) + [""]=> + int(5) + [-1]=> + int(6) +} +object(stdClass)#%d (5) { + ["a"]=> + string(7) "changed" + ["0"]=> + int(3) + [""]=> + int(5) + ["-1"]=> + int(6) + ["extra"]=> + bool(true) +} +array(1) { + [1]=> + array(2) { + [0]=> + object(stdClass)#%d (1) { + ["v"]=> + int(2) + } + [1]=> + object(stdClass)#%d (1) { + ["v"]=> + int(3) + } + } +}

« previous php.cvs (#144128) next »