Skip to content

Commit 1687231

Browse files
authored
ext/spl: Deprecate SplObjectStorage::contains(), SplObjectStorage::attach(), and SplObjectStorage::detach() (#19424)
RFC: https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_splobjectstoragecontains_splobjectstorageattach_and_splobjectstoragedetach
1 parent 1e108e9 commit 1687231

23 files changed

+80
-46
lines changed

Zend/tests/enum/spl-object-storage.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ $storage[Foo::Baz] = 'Baz';
1616
var_dump($storage[Foo::Bar]);
1717
var_dump($storage[Foo::Baz]);
1818

19-
var_dump($storage->contains(Foo::Bar));
20-
var_dump($storage->contains(Foo::Qux));
19+
var_dump($storage->offsetExists(Foo::Bar));
20+
var_dump($storage->offsetExists(Foo::Qux));
2121

2222
$serialized = serialize($storage);
2323
var_dump($serialized);

ext/spl/spl_observer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "zend_smart_str.h"
2626
#include "zend_interfaces.h"
2727
#include "zend_exceptions.h"
28+
#include "zend_attributes.h"
2829

2930
#include "php_spl.h" /* For php_spl_object_hash() */
3031
#include "spl_observer.h"

ext/spl/spl_observer.stub.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@ public function notify(): void;
2323
class SplObjectStorage implements Countable, SeekableIterator, Serializable, ArrayAccess
2424
{
2525
/** @tentative-return-type */
26+
#[\Deprecated(since: '8.5', message: "use method SplObjectStorage::offsetSet() instead")]
2627
public function attach(object $object, mixed $info = null): void {}
2728

2829
/** @tentative-return-type */
30+
#[\Deprecated(since: '8.5', message: "use method SplObjectStorage::offsetUnset() instead")]
2931
public function detach(object $object): void {}
3032

3133
/** @tentative-return-type */
34+
#[\Deprecated(since: '8.5', message: "use method SplObjectStorage::offsetExists() instead")]
3235
public function contains(object $object): bool {}
3336

3437
/** @tentative-return-type */

ext/spl/spl_observer_arginfo.h

Lines changed: 26 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/spl/tests/SplObjectStorage/SplObjectStorage_coalesce.phpt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@ var_dump(isset($s[$o2]));
1919
var_dump(empty($s[$o2]));
2020
$s[$o2] = null;
2121
var_dump($s[$o2] ?? new stdClass());
22-
echo "check isset/empty/contains for null. offsetExists returns true as long as the entry is there.\n";
22+
echo "check isset/empty/contains/offsetExists for null. offsetExists returns true as long as the entry is there.\n";
2323
var_dump(isset($s[$o2]));
2424
var_dump(empty($s[$o2]));
2525
var_dump($s->contains($o2));
26-
echo "check isset/empty/contains for false.\n";
26+
var_dump($s->offsetExists($o2));
27+
echo "check isset/empty/contains/offsetExists for false.\n";
2728
$s[$o2] = false;
2829
var_dump(isset($s[$o2]));
2930
var_dump(empty($s[$o2]));
3031
var_dump($s->contains($o2));
32+
var_dump($s->offsetExists($o2));
3133
try {
3234
$s['invalid'] = 123;
3335
} catch (Error $e) {
@@ -56,18 +58,24 @@ bool(false)
5658
bool(true)
5759
object(stdClass)#4 (0) {
5860
}
59-
check isset/empty/contains for null. offsetExists returns true as long as the entry is there.
61+
check isset/empty/contains/offsetExists for null. offsetExists returns true as long as the entry is there.
6062
bool(true)
6163
bool(true)
64+
65+
Deprecated: Method SplObjectStorage::contains() is deprecated since 8.5, use method SplObjectStorage::offsetExists() instead in %s on line %d
66+
bool(true)
6267
bool(true)
63-
check isset/empty/contains for false.
68+
check isset/empty/contains/offsetExists for false.
6469
bool(true)
6570
bool(true)
71+
72+
Deprecated: Method SplObjectStorage::contains() is deprecated since 8.5, use method SplObjectStorage::offsetExists() instead in %s on line %d
73+
bool(true)
6674
bool(true)
6775
TypeError: SplObjectStorage::offsetSet(): Argument #1 ($object) must be of type object, string given
6876
TypeError: SplObjectStorage::offsetExists(): Argument #1 ($object) must be of type object, string given
6977

70-
Notice: Indirect modification of overloaded element of SplObjectStorage has no effect in %s on line 38
78+
Notice: Indirect modification of overloaded element of SplObjectStorage has no effect in %s on line %d
7179
object(SplObjectStorage)#1 (1) {
7280
["storage":"SplObjectStorage":private]=>
7381
array(2) {

ext/spl/tests/SplObjectStorage/SplObjectStorage_removeAllExcept_basic.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ $b = (object) 'b';
1010
$c = (object) 'c';
1111

1212
$foo = new SplObjectStorage;
13-
$foo->attach($a);
14-
$foo->attach($b);
13+
$foo->offsetSet($a);
14+
$foo->offsetSet($b);
1515

1616
$bar = new SplObjectStorage;
17-
$bar->attach($b);
18-
$bar->attach($c);
17+
$bar->offsetSet($b);
18+
$bar->offsetSet($c);
1919

2020
$foo->removeAllExcept($bar);
21-
var_dump($foo->contains($a));
22-
var_dump($foo->contains($b));
21+
var_dump($foo->offsetExists($a));
22+
var_dump($foo->offsetExists($b));
2323

2424
?>
2525
--EXPECT--

ext/spl/tests/SplObjectStorage_seek.phpt renamed to ext/spl/tests/SplObjectStorage/SplObjectStorage_seek.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ var_dump($storage->current());
6464

6565
echo "--- With holes cases ---\n";
6666

67-
$storage->detach($b);
68-
$storage->detach($d);
67+
$storage->offsetUnset($b);
68+
$storage->offsetUnset($d);
6969

7070
foreach (range(0, 2) as $index) {
7171
$storage->seek($index);

ext/spl/tests/SplObjectStorage/bug49263.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ $o2 = new stdClass;
77

88
$s = new SplObjectStorage();
99

10-
$s->attach($o1, array('prev' => 2, 'next' => $o2));
11-
$s->attach($o2, array('prev' => $o1));
10+
$s->offsetSet($o1, array('prev' => 2, 'next' => $o2));
11+
$s->offsetSet($o2, array('prev' => $o1));
1212

1313
$ss = serialize($s);
1414
unset($s,$o1,$o2);

ext/spl/tests/SplObjectStorage/bug53071.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ function LimitedScope()
1313
$myB = new SplObjectStorage();
1414
$myC = new myClass();
1515
$myC->member = $myA; // myC has a reference to myA
16-
$myB->Attach($myC); // myB attaches myC
17-
$myA->member = $myB; // myA has myB, comleting the cycle
16+
$myB->offsetSet($myC); // myB attaches myC
17+
$myA->member = $myB; // myA has myB, completing the cycle
1818
}
1919
LimitedScope();
2020
var_dump(gc_collect_cycles());

ext/spl/tests/SplObjectStorage/bug67582.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class MyObjectStorage extends SplObjectStorage {
1111
class TestObject {}
1212

1313
$list = new MyObjectStorage();
14-
$list->attach(new TestObject());
14+
$list->offsetSet(new TestObject());
1515

1616
foreach($list as $x) var_dump($list->offsetExists($x));
1717

0 commit comments

Comments
 (0)