PHP 8.6 deprecates spl_object_hash(). Use spl_object_id() when you only need a temporary integer identity during an object’s lifetime. When associating metadata with objects, prefer an object-keyed structure instead of managing IDs yourself.
Last updated: October 12, 2026.
<?php
declare(strict_types=1);
final class Metadata
{
/** @var WeakMap<object, array<string, mixed>> */
private WeakMap $values;
public function __construct()
{
$this->values = new WeakMap();
}
public function set(object $object, array $data): void
{
$this->values[$object] = $data;
}
public function get(object $object): ?array
{
return $this->values[$object] ?? null;
}
}A WeakMap entry disappears when its object key is garbage-collected, which is useful for caches and derived metadata in long-running processes. Use a normal object property when the data belongs to the class itself.
Choose the replacement by purpose
The PHP 8.6 deprecations RFC explains that spl_object_hash() and spl_object_id() have equivalent identity semantics in current PHP, while the latter returns a clearer integer handle. A direct replacement is appropriate for temporary maps that are fully discarded before any object ID can be reused.
The official WeakMap documentation describes an object-keyed dictionary that does not keep objects alive. If entries should keep their objects alive, use an object storage collection or redesign the data ownership rather than converting handles into permanent identifiers.
Object identity is not persistent identity
Both object hashes and object IDs identify a particular in-memory instance. They are not derived from property values, are not stable between requests, and may be reused after an object is destroyed. Do not store them in a database, expose them as public record IDs, or use them as security tokens.
Search dependencies as well as application code when testing PHP 8.6. Upgrade maintained packages before patching vendor files, and run the test suite with deprecations visible. Continue with PHP deprecation cleanup, PHP arrays, and practical PHP development habits.