mirror of
https://github.com/php/php-src.git
synced 2026-03-24 16:22:37 +01:00
This makes reading/writing with `$splObjectStorage[$offset]` shorthand twice as fast as it was previously, and a bit faster than offsetGet/offsetSet instead of (previously) much slower. Call destructor after overriding old SplObjectStorage entry. Previously, it was called before, which was possibly unsafe if the destructor had side effects. Add tests. Closes GH-7695 Related to GH-7690 Check for ref in SplObjectStorage->__unserialize, check for ref. SplObjectStorage->unserialize may be a different cause of references for malformed inputs, so continue checking In internally used methods, convert references to non-references if they're found.
91 lines
2.0 KiB
PHP
91 lines
2.0 KiB
PHP
--TEST--
|
|
SplObjectStorage magic operators
|
|
--FILE--
|
|
<?php
|
|
$i = 'dynamic';
|
|
$v = $i . '_';
|
|
$s = new SplObjectStorage();
|
|
$o1 = new Stdclass;
|
|
$o2 = new Stdclass;
|
|
var_dump($s[$o1] ?? 'default');
|
|
var_dump($s[$o1] ??= $i);
|
|
var_dump($s[$o1] ??= null);
|
|
var_dump($s[$o1] ??= false);
|
|
var_dump($s[$o1] ?? $i);
|
|
var_dump(isset($s[$o1]));
|
|
var_dump(empty($s[$o1]));
|
|
echo "o2\n";
|
|
var_dump(isset($s[$o2]));
|
|
var_dump(empty($s[$o2]));
|
|
$s[$o2] = null;
|
|
var_dump($s[$o2] ?? new stdClass());
|
|
echo "check isset/empty/contains for null. offsetExists returns true as long as the entry is there.\n";
|
|
var_dump(isset($s[$o2]));
|
|
var_dump(empty($s[$o2]));
|
|
var_dump($s->contains($o2));
|
|
echo "check isset/empty/contains for false.\n";
|
|
$s[$o2] = false;
|
|
var_dump(isset($s[$o2]));
|
|
var_dump(empty($s[$o2]));
|
|
var_dump($s->contains($o2));
|
|
try {
|
|
$s['invalid'] = 123;
|
|
} catch (Error $e) {
|
|
printf("%s: %s\n", $e::class, $e->getMessage());
|
|
}
|
|
try {
|
|
var_dump(isset($s['invalid']));
|
|
} catch (Error $e) {
|
|
printf("%s: %s\n", $e::class, $e->getMessage());
|
|
}
|
|
$a = &$s[$o1];
|
|
|
|
var_dump($s);
|
|
|
|
?>
|
|
--EXPECTF--
|
|
string(7) "default"
|
|
string(7) "dynamic"
|
|
string(7) "dynamic"
|
|
string(7) "dynamic"
|
|
string(7) "dynamic"
|
|
bool(true)
|
|
bool(false)
|
|
o2
|
|
bool(false)
|
|
bool(true)
|
|
object(stdClass)#4 (0) {
|
|
}
|
|
check isset/empty/contains for null. offsetExists returns true as long as the entry is there.
|
|
bool(true)
|
|
bool(true)
|
|
bool(true)
|
|
check isset/empty/contains for false.
|
|
bool(true)
|
|
bool(true)
|
|
bool(true)
|
|
TypeError: SplObjectStorage::offsetSet(): Argument #1 ($object) must be of type object, string given
|
|
TypeError: SplObjectStorage::offsetExists(): Argument #1 ($object) must be of type object, string given
|
|
|
|
Notice: Indirect modification of overloaded element of SplObjectStorage has no effect in %s on line 38
|
|
object(SplObjectStorage)#1 (1) {
|
|
["storage":"SplObjectStorage":private]=>
|
|
array(2) {
|
|
[0]=>
|
|
array(2) {
|
|
["obj"]=>
|
|
object(stdClass)#2 (0) {
|
|
}
|
|
["inf"]=>
|
|
string(7) "dynamic"
|
|
}
|
|
[1]=>
|
|
array(2) {
|
|
["obj"]=>
|
|
object(stdClass)#3 (0) {
|
|
}
|
|
["inf"]=>
|
|
bool(false)
|
|
}
|
|
}
|
|
}
|