Skip to content

Implement spl_object_id(object $x) : int #2611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ PHP NEWS
- Sodium:
. New cryptographic extension

- SPL:
. Added spl_object_id() function returning an integer (the object handle) for an object.
(Tyson Andre)

- SQLite3:
. Fixed bug #74883 (SQLite3::__construct() produces "out of memory" exception
with invalid flags). (Anatol)
Expand Down
19 changes: 19 additions & 0 deletions ext/spl/php_spl.c
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,20 @@ PHP_FUNCTION(spl_object_hash)
}
/* }}} */

/* {{{ proto int spl_object_id(object obj)
Returns the integer object handle for the given object */
PHP_FUNCTION(spl_object_id)
{
zval *obj;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_OBJECT(obj)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_NULL());

RETURN_LONG((zend_long)Z_OBJ_HANDLE_P(obj));
}
/* }}} */

PHPAPI zend_string *php_spl_object_hash(zval *obj) /* {{{*/
{
intptr_t hash_handle, hash_handlers;
Expand Down Expand Up @@ -915,6 +929,10 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_spl_object_hash, 0, 0, 1)
ZEND_ARG_INFO(0, obj)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_spl_object_id, 0, 0, 1)
ZEND_ARG_INFO(0, obj)
ZEND_END_ARG_INFO()
/* }}} */

/* {{{ spl_functions
Expand All @@ -931,6 +949,7 @@ const zend_function_entry spl_functions[] = {
PHP_FE(class_implements, arginfo_class_implements)
PHP_FE(class_uses, arginfo_class_uses)
PHP_FE(spl_object_hash, arginfo_spl_object_hash)
PHP_FE(spl_object_id, arginfo_spl_object_id)
#ifdef SPL_ITERATORS_H
PHP_FE(iterator_to_array, arginfo_iterator_to_array)
PHP_FE(iterator_count, arginfo_iterator)
Expand Down
24 changes: 24 additions & 0 deletions ext/spl/tests/spl_object_id.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
SPL: spl_object_id()
--FILE--
<?php

var_dump(spl_object_id(new stdClass));
var_dump(spl_object_id(42));
var_dump(spl_object_id());
$a = new stdClass();
var_dump(spl_object_id(new stdClass) === spl_object_id($a));

?>
===DONE===
<?php exit(0); ?>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not necessary and hides leaks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, removed. It's fairly common in ext/spl/tests, so I thought it was some sort of convention.

This was based on spl_object_hash.phpt, changing that is out of scope.

--EXPECTF--
int(%d)

Warning: spl_object_id() expects parameter 1 to be object, integer given in %sspl_object_id.php on line %d
NULL

Warning: spl_object_id() expects exactly 1 parameter, 0 given in %sspl_object_id.php on line %d
NULL
bool(false)
===DONE===