spl_object_id
spl_object_id(object $object): int
Unique integer handle for an object: same object always yields the same id, distinct objects differ (the numeric id is not pre-determined, so the example asserts booleans); identity-vs-equality compared across languages; replaces spl_object_hash (deprecated since 8.6).
C C23
#include <stdio.h>
#include <string.h>
typedef struct { char name[16]; } user;
int main(void) {
user a = {"ann"};
user b = {"ann"};
user *c = &a;
printf("%d\n", (void *)&a == (void *)&a); /* same object, same identity */
printf("%d\n", (void *)c == (void *)&a); /* c points at the same object */
printf("%d\n", strcmp(a.name, b.name) == 0); /* equal by value (field compare) */
printf("%d\n", (void *)&a == (void *)&b); /* different objects */
return 0;
}
1 1 1 0
C++ C++26
#include <iostream>
#include <string>
struct User {
std::string name;
bool operator==(const User&) const = default; // C++20 value comparison
};
int main() {
User a{"ann"};
User b{"ann"};
User *c = &a;
std::cout << std::boolalpha;
std::cout << (&a == &a) << "\n"; /* same object */
std::cout << (&a == c) << "\n"; /* c points at the same object */
std::cout << (a == b) << "\n"; /* equal by value */
std::cout << (&a == &b) << "\n"; /* different objects */
return 0;
}
true true true false
C# v11.0.100
// RuntimeHelpers.GetHashCode gives the reference-identity hash
var a = new System.Text.StringBuilder("hi").ToString();
var b = new System.Text.StringBuilder("hi").ToString();
var c = a;
Console.WriteLine(System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(a) ==
System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(a));
Console.WriteLine(System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(a) ==
System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(c)); // same object
Console.WriteLine(a == b); // value equality: True
Console.WriteLine(ReferenceEquals(a, b)); // identity: different objects
True True True False
Go v1.27.4
package main
import "fmt"
type user struct{ name string }
func main() {
a := &user{"ann"}
b := &user{"ann"}
c := a
fmt.Println(a == a) // pointer equality: the same object
fmt.Println(a == c) // c points at the same object
fmt.Println(*a == *b) // struct equality by value
fmt.Println(a == b) // different objects
}
true true true false
Java v27.0.1
public class Main {
public static void main(String[] args) {
String a = new String("hi"); // fresh instances, not interned
String b = new String("hi");
String c = a;
System.out.println(System.identityHashCode(a) == System.identityHashCode(a));
System.out.println(System.identityHashCode(a) == System.identityHashCode(c)); // same object
System.out.println(a.equals(b)); // value equality
System.out.println(a == b); // identity: different objects
}
}
true true true false
JavaScript v26.13.1
// JS has no object id; the WeakMap-counter idiom is the standard substitute
let counter = 0;
const ids = new WeakMap();
const idOf = (o) => {
if (!ids.has(o)) ids.set(o, ++counter); // lazily assign a serial per identity
return ids.get(o);
};
const a = { name: "ann" };
const b = { name: "ann" };
const c = a;
console.log(idOf(a) === idOf(a));
console.log(idOf(a) === idOf(c)); // the same object
console.log(JSON.stringify(a) === JSON.stringify(b)); // value equality
console.log(idOf(a) === idOf(b)); // different objects
true true true false
PHP v8.6.0
// Object identity vs equality: the id number itself is not printable
// (it depends on the process), so the demo asserts derived facts.
$a = new stdClass();
$b = new stdClass();
$c = $a; // $c and $a are the SAME object
var_dump(spl_object_id($a) === spl_object_id($a)); // same object, same id
var_dump(spl_object_id($a) === spl_object_id($c)); // same object, same id
var_dump($a == $b); // equal by value (both empty)
var_dump($a === $b); // but different objects
bool(true) bool(true) bool(true) bool(false)
Python v3.15.2
# id() is the native counterpart (the PyObject* address on CPython)
a = [1, 2]
b = [1, 2]
c = a
print(id(a) == id(a))
print(id(a) == id(c)) # the same object
print(a == b) # value equality
print(id(a) == id(b)) # different objects
True True True False
Rust v1.99.1
#[derive(PartialEq)]
struct User { name: String }
fn main() {
// Rust has no runtime object id: identity is just a memory address
let a = User { name: "ann".to_string() };
let b = User { name: "ann".to_string() };
let c = &a;
println!("{}", std::ptr::eq(&a, &a)); // same object
println!("{}", std::ptr::eq(&a, c)); // c borrows the same object
println!("{}", a == b); // value equality (PartialEq)
println!("{}", std::ptr::eq(&a, &b)); // different objects
}
true true true false
TypeScript v26.13.1
// TS has no identity id either: the same WeakMap idiom, typed
let counter = 0;
const ids = new WeakMap<object, number>();
function idOf(o: object): number {
if (!ids.has(o)) ids.set(o, ++counter);
return ids.get(o)!;
}
const a: object = { name: "ann" };
const b: object = { name: "ann" };
const c = a;
console.log(idOf(a) === idOf(a));
console.log(idOf(a) === idOf(c)); // the same object
console.log(JSON.stringify(a) === JSON.stringify(b)); // value equality
console.log(idOf(a) === idOf(b)); // different objects
true true true false
Language differences
C: no objects — identity is pointer equality (&a == c); the equality demo compares a field with strcmp (note: memcmp on the whole struct would read padding bytes — a classic pitfall).
C++: same as C — &a == c for identity, operator== for equality (the example uses a C++20 defaulted comparison).
C#: RuntimeHelpers.GetHashCode gives the reference-identity hash (object.GetHashCode can be overridden and is not the same); the equality demo uses two equal-content string instances (== is value comparison). ReferenceEquals(a, b) is the more explicit identity check.
Go: no object id — pointer equality is identity (a == c), and structs compare by value with == (allowed when fields are comparable).
Java: System.identityHashCode corresponds to spl_object_id (note it is not guaranteed unique — only same-object-same-code; use == for identity); equals is value equality.
JavaScript: no object id — the idiom is a WeakMap counter: lazily assign a serial per object identity, and the weak key never prevents GC; value equality via JSON.stringify or a deep compare.
PHP: spl_object_id() (7.2+) returns the object's integer handle within the current process — the same object yields the same value on every call, distinct objects differ (the example asserts booleans instead of printing ids, since the numeric value itself is not pre-determined); ids may be reused after the object is destroyed (documented, same as spl_object_hash(), deprecated since 8.6). Identity vs equality: $a == $b compares property values, $a === $b compares whether they are the same object — two empty stdClass instances are == but not ===. Since 8.6 prefer spl_object_id over the deprecated spl_object_hash() (whose string is just an encoding of "object-id" . ":class-name"; the manual recommends id).
Python: id() is the native counterpart — it returns the object address (the PyObject* on CPython); is is identity, == is value equality (list content comparison).
Rust: no runtime object id — the language only has memory addresses (std::ptr::eq), and the compiler may move or elide objects; "identity" fades into ownership; the example uses ptr::eq for identity and PartialEq for value comparison.
TypeScript: same runtime as the JS WeakMap idiom; the object type carries no value-semantics information.