Skip to content

ext/spl: Deprecate SplObjectStorage::contains(), SplObjectStorage::attach(), and SplObjectStorage::detach()#19424

Merged
Girgias merged 1 commit into
php:masterfrom
Girgias:8.5-dep-spl-object-storage
Aug 9, 2025
Merged

ext/spl: Deprecate SplObjectStorage::contains(), SplObjectStorage::attach(), and SplObjectStorage::detach()#19424
Girgias merged 1 commit into
php:masterfrom
Girgias:8.5-dep-spl-object-storage

Conversation

@Girgias

@Girgias Girgias commented Aug 8, 2025

Copy link
Copy Markdown
Member

@Girgias Girgias requested a review from kocsismate as a code owner August 8, 2025 21:04
@Girgias Girgias merged commit 1687231 into php:master Aug 9, 2025
9 checks passed
@Girgias Girgias deleted the 8.5-dep-spl-object-storage branch August 9, 2025 10:36
sorinsarca pushed a commit to opis/closure that referenced this pull request Oct 14, 2025
* Prefer `offsetSet` over deprecated `attach`

`SplObjectStorage::attach()` is deprecated since
PHP v8.5; `SplObjectStorage::offsetSet()` should
be used instead [1].

[1]: php/php-src#19424
@dg

dg commented Nov 3, 2025

Copy link
Copy Markdown

Folks, I'm very glad about the effort to Improve language coherence for the behaviour of offsets and containers because there really are many inconsistencies here. However, I'm concerned that this change is a step in the wrong direction.

  1. SplObjectStorage has a truly poor API. It contains methods for iteration that, in my opinion, must confuse programmers. It implements ArrayAccess in an inappropriate way. The strangest thing is:
$storage[$obj] = 'value';

foreach ($storage as $key => $value) // $key = 'value' and $value = $obj

This is very unfortunate.

The only thing about the SplObjectStorage API that is truly nice, elegant, and correct are the attach(), contains(), and detach() methods. These are genuinely clear and understandable. And this change kills them.

  1. Methods of the ArrayAccess interface are fundamentally not expected to be called directly. They are like internal methods. I don't even want them offered by autocomplete in editors, because their sole purpose is to enable syntactic sugar in the form of array-like access. This change directly encourages users to call the offset*() method.

  2. The method names like offsetSet() are completely meaningless and unintuitive from the perspective of the Object Storage class. What offset?

I see this as a major step backward. If the goal is to solve the problem with method overriding in child classes, a better solution can certainly be found.

Therefore, I ask you to please keep the attach(), contains(), and detach() methods as they are until a better solution is found. // cc @Girgias

kelunik pushed a commit to amphp/parallel that referenced this pull request Nov 15, 2025
`SplObjectStorage::attach()` was deprecated with PHP 8.5 [1].
Its usage is replaced by `SplObjectStorage::offsetSet()`,
which provides the same functionality.

[1]: php/php-src#19424
sorinsarca added a commit to opis/closure that referenced this pull request Nov 20, 2025
* Added php 8.5 tests

* Updated keywords

* Prefer `offsetSet` over deprecated `attach` (#161)

* Prefer `offsetSet` over deprecated `attach`

`SplObjectStorage::attach()` is deprecated since
PHP v8.5; `SplObjectStorage::offsetSet()` should
be used instead [1].

[1]: php/php-src#19424

* Added test

* Updated README

---------

Co-authored-by: Elias Häußler <elias@haeussler.dev>
@danog

danog commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

+1 on @dg's comment, this is really counterintuitive, one of the most actively harmful and counterproductive deprecations in 8.5 IMO

@Girgias

Girgias commented Jun 1, 2026

Copy link
Copy Markdown
Member Author

It is not possible to guarantee that the implementation of attach/detach/contains matches the implementation of offsetSet/offsetUnset/offsetExists as the class is not final.

Moreover, this is not the place to discuss language changes, if you want to revert this decision please submit an RFC.

@danog

danog commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

It is not possible to guarantee that the implementation of attach/detach/contains matches the implementation of offsetSet/offsetUnset/offsetExists as the class is not final.

IMO, this is not a bad enough problem to warrant removal of a clearly superior set of methods in terms of DX (actually, it's not really a problem, it's absolutely logical that if you don't override a method, its behavior doesn't change).

I won't be submitting an RFC to revert this, but I do feel like the recent deprecations in 8.5 really went overboard a bit, and I'd kindly recommend being a bit more considerate in future deprecations.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants