Skip to content

Commit f436d52

Browse files
committed
Implement spl_object_id(object $x) : int
spl_object_id is a new function returning the object handle, as a signed integer. Discussion for this new function is ongoing on php-internals, see https://marc.info/?t=143835274500003&r=1&w=2 The object id is unique for the lifetime of the object. When the object is garbage collected, different objects may & will have the same object id. - This is also the case for the string generated by spl_object_hash It is always possible to cast the object handle to a **signed** zend_long in php 7.2. _zend_object->handle is always of the type `uint32_t`. (zend_long is 32 bits on 32 bit builds, 64 bits on 64 bit builds) As of php 7.0, the object id uniquely identifies the object, there can't be two objects with the same id but different handlers (See the implementation of spl_object_hash) Skip the pointless XORing, as discussed in internals. - It was intended to avoid exposing in-memory addresses. - The object handle is not a memory address. - The output of var_dump() includes the object handle(id) Commit a unit test of spl_object_id Update NEWS
1 parent da2583b commit f436d52

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ PHP NEWS
4545
. Fixed bug #74873 (Minor BC break: PCRE_JIT changes output of preg_match()).
4646
(Dmitry)
4747

48+
- SPL:
49+
. Added spl_object_id() function returning an integer (the object handle) for an object.
50+
(Tyson Andre)
51+
4852
- SQLite3:
4953
. Fixed bug #74883 (SQLite3::__construct() produces "out of memory" exception
5054
with invalid flags). (Anatol)

ext/spl/php_spl.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,20 @@ PHP_FUNCTION(spl_object_hash)
794794
}
795795
/* }}} */
796796

797+
/* {{{ proto int spl_object_id(object obj)
798+
Returns the integer object handle for the given object */
799+
PHP_FUNCTION(spl_object_id)
800+
{
801+
zval *obj;
802+
803+
ZEND_PARSE_PARAMETERS_START(1, 1)
804+
Z_PARAM_OBJECT(obj)
805+
ZEND_PARSE_PARAMETERS_END_EX(RETURN_NULL());
806+
807+
RETURN_LONG((zend_long)Z_OBJ_HANDLE_P(obj));
808+
}
809+
/* }}} */
810+
797811
PHPAPI zend_string *php_spl_object_hash(zval *obj) /* {{{*/
798812
{
799813
intptr_t hash_handle, hash_handlers;
@@ -915,6 +929,10 @@ ZEND_END_ARG_INFO()
915929
ZEND_BEGIN_ARG_INFO_EX(arginfo_spl_object_hash, 0, 0, 1)
916930
ZEND_ARG_INFO(0, obj)
917931
ZEND_END_ARG_INFO()
932+
933+
ZEND_BEGIN_ARG_INFO_EX(arginfo_spl_object_id, 0, 0, 1)
934+
ZEND_ARG_INFO(0, obj)
935+
ZEND_END_ARG_INFO()
918936
/* }}} */
919937

920938
/* {{{ spl_functions
@@ -931,6 +949,7 @@ const zend_function_entry spl_functions[] = {
931949
PHP_FE(class_implements, arginfo_class_implements)
932950
PHP_FE(class_uses, arginfo_class_uses)
933951
PHP_FE(spl_object_hash, arginfo_spl_object_hash)
952+
PHP_FE(spl_object_id, arginfo_spl_object_id)
934953
#ifdef SPL_ITERATORS_H
935954
PHP_FE(iterator_to_array, arginfo_iterator_to_array)
936955
PHP_FE(iterator_count, arginfo_iterator)

ext/spl/tests/spl_object_id.phpt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
SPL: spl_object_id()
3+
--FILE--
4+
<?php
5+
6+
var_dump(spl_object_id(new stdClass));
7+
var_dump(spl_object_id(42));
8+
var_dump(spl_object_id());
9+
$a = new stdClass();
10+
var_dump(spl_object_id(new stdClass) === spl_object_id($a));
11+
12+
?>
13+
===DONE===
14+
<?php exit(0); ?>
15+
--EXPECTF--
16+
int(%d)
17+
18+
Warning: spl_object_id() expects parameter 1 to be object, integer given in %sspl_object_id.php on line %d
19+
NULL
20+
21+
Warning: spl_object_id() expects exactly 1 parameter, 0 given in %sspl_object_id.php on line %d
22+
NULL
23+
bool(false)
24+
===DONE===

0 commit comments

Comments
 (0)