Skip to content

Commit 438efc5

Browse files
committed
PHPC-1622: Use get_properties_for handler in ObjectId (prototype)
Replace the get_debug_info and get_properties handlers with a single get_properties_for handler that dispatches by purpose: - ZEND_PROP_PURPOSE_DEBUG, ARRAY_CAST, GET_OBJECT_VARS, VAR_EXPORT: return a fresh temporary HashTable (no caching) - ZEND_PROP_PURPOSE_SERIALIZE, JSON: return NULL, deferring to the class's __serialize/__unserialize and JsonSerializable implementations The get_properties handler is retained since foreach on non-Traversable objects calls it directly, bypassing get_properties_for on all PHP versions. ZEND_PROP_PURPOSE_GET_OBJECT_VARS (added in PHP 8.4 as part of the Property Hooks RFC) is guarded with #if PHP_VERSION_ID >= 80400; on PHP 8.1-8.3 get_object_vars() reaches get_properties directly. Add tests for the two previously untested purposes: - bson-objectid-array_cast-001.phpt (ZEND_PROP_PURPOSE_ARRAY_CAST) - bson-objectid-var_export-001.phpt (ZEND_PROP_PURPOSE_VAR_EXPORT)
1 parent bb72111 commit 438efc5

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

src/BSON/ObjectId.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,8 @@ static int phongo_objectid_compare_objects(zval* o1, zval* o2)
252252
return strcmp(intern1->oid, intern2->oid);
253253
}
254254

255-
static HashTable* phongo_objectid_get_debug_info(zend_object* object, int* is_temp)
255+
static HashTable* phongo_objectid_get_properties_for(zend_object* object, ARG_UNUSED zend_prop_purpose purpose)
256256
{
257-
*is_temp = 1;
258257
return phongo_objectid_get_properties_hash(object, true);
259258
}
260259

@@ -269,12 +268,12 @@ void phongo_objectid_init_ce(INIT_FUNC_ARGS)
269268
phongo_objectid_ce->create_object = phongo_objectid_create_object;
270269

271270
memcpy(&phongo_handler_objectid, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
272-
phongo_handler_objectid.compare = phongo_objectid_compare_objects;
273-
phongo_handler_objectid.clone_obj = phongo_objectid_clone_object;
274-
phongo_handler_objectid.get_debug_info = phongo_objectid_get_debug_info;
275-
phongo_handler_objectid.get_properties = phongo_objectid_get_properties;
276-
phongo_handler_objectid.free_obj = phongo_objectid_free_object;
277-
phongo_handler_objectid.offset = XtOffsetOf(phongo_objectid_t, std);
271+
phongo_handler_objectid.compare = phongo_objectid_compare_objects;
272+
phongo_handler_objectid.clone_obj = phongo_objectid_clone_object;
273+
phongo_handler_objectid.get_properties_for = phongo_objectid_get_properties_for;
274+
phongo_handler_objectid.get_properties = phongo_objectid_get_properties;
275+
phongo_handler_objectid.free_obj = phongo_objectid_free_object;
276+
phongo_handler_objectid.offset = XtOffsetOf(phongo_objectid_t, std);
278277
}
279278

280279
bool phongo_objectid_new(zval* return_value, const bson_oid_t* oid)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
MongoDB\BSON\ObjectId array cast (ZEND_PROP_PURPOSE_ARRAY_CAST)
3+
--FILE--
4+
<?php
5+
6+
$oid = new MongoDB\BSON\ObjectId('53e2a1c40640fd72175d4603');
7+
8+
var_dump((array) $oid);
9+
10+
?>
11+
===DONE===
12+
<?php exit(0); ?>
13+
--EXPECT--
14+
array(1) {
15+
["oid"]=>
16+
string(24) "53e2a1c40640fd72175d4603"
17+
}
18+
===DONE===
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
MongoDB\BSON\ObjectId var_export() (ZEND_PROP_PURPOSE_VAR_EXPORT)
3+
--FILE--
4+
<?php
5+
6+
$oid = new MongoDB\BSON\ObjectId('53e2a1c40640fd72175d4603');
7+
8+
var_export($oid);
9+
echo "\n";
10+
11+
?>
12+
===DONE===
13+
<?php exit(0); ?>
14+
--EXPECTF--
15+
%r\\?%rMongoDB\BSON\ObjectId::__set_state(array(
16+
'oid' => '53e2a1c40640fd72175d4603',
17+
))
18+
===DONE===

0 commit comments

Comments
 (0)