php-src/ext/spl/tests/spl_object_id.phpt
Tyson Andre 5097e2ee13 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)
2017-08-02 17:54:07 +02:00

22 lines
474 B
PHP

--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));
?>
--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)