Skip to content

Fix spl_object_hash deprecation on PHP 8.6 - #254

Open
jtojnar wants to merge 2 commits into
php-http:2.xfrom
jtojnar:php86-deprecation
Open

Fix spl_object_hash deprecation on PHP 8.6#254
jtojnar wants to merge 2 commits into
php-http:2.xfrom
jtojnar:php86-deprecation

Conversation

@jtojnar

@jtojnar jtojnar commented Sep 3, 2026

Copy link
Copy Markdown
Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Related tickets N/A
Documentation N/A
License MIT

What's in this PR?

Switch to the suggested non-deprecated function where supported.

https://www.php.net/manual/en/function.spl-object-hash.php
https://www.php.net/manual/en/function.spl-object-id.php

Why?

When testing with PHP 8.6.0beta2, I get the following deprecation warning:

Function spl_object_hash() is deprecated since 8.6, consider using spl_object_id() instead

Example Usage

N/A

Checklist

  • Updated CHANGELOG.md to describe BC breaks / deprecations | new feature | bugfix
  • Documentation pull request created (if not simply a bugfix) – N/A bugfix

The 1.x branch has long been abandoned and it contains many security issues
but we cannot switch to 2.x while we still support PHP 7.1.
It is only used for tests so it should be safe enough.
When testing with PHP 8.6.0beta2, I get the following deprecation warning:

    Function spl_object_hash() is deprecated since 8.6, consider using spl_object_id() instead

Let’s switch to the suggested function where supported.

https://www.php.net/manual/en/function.spl-object-hash.php
https://www.php.net/manual/en/function.spl-object-id.php
@jtojnar
jtojnar force-pushed the php86-deprecation branch 2 times, most recently from e0fa0b8 to 52920f9 Compare September 3, 2026 03:02
$uri = $this->createUri($response, $request);
$redirectRequest = $this->buildRedirectRequest($request, $uri, $statusCode);
$chainIdentifier = spl_object_hash((object) $first);
$chainIdentifier = \PHP_VERSION_ID < 70200 ? spl_object_hash((object) $first) : (string) spl_object_id((object) $first);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I am surprised this ever worked. According to docs:

This id can be used as a hash key for storing objects, or for identifying an object, as long as the object is not destroyed. Once the object is destroyed, its hash may be reused for other objects.

And indeed, running:

var_dump(spl_object_hash((object) ['a', 'b']));
var_dump(spl_object_hash((object) ['a', 'b', 'c']));
var_dump(spl_object_hash((object) md5(...)));

Prints the following for me:

string(32) "00000000000000020000000000000000"
string(32) "00000000000000020000000000000000"
string(32) "00000000000000020000000000000000"

https://3v4l.org/KTQqT4#veol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant