虽然 `offsetSet` 应该与 `attach` 别名,但实际结果并不表明这一点。扩展 SplObjectStorage 类时,预计对 `attach` 的修改也会影响 `offsetSet`。但是,似乎它们都需要扩展。请考虑以下结果...
<?php
declare(strict_types=1);
class CustomSplObjectStorage extends SplObjectStorage
{
public function offsetSet(mixed $object, mixed $info = null): void
{
print("offsetSet called\n");
parent::offsetSet($object, $info);
}
public function attach(mixed $object, mixed $info = null): void
{
print("attach called\n");
parent::attach($object, $info);
}
}
$a = new CustomSplObjectStorage();
$a[new stdClass()] = 'ok';
$a->attach(new stdClass(), 'ok');
?>
这将打印
```
offsetSet called
attach called
```
虽然我们预期...
```
offsetSet called
attach called
attach called
```
... 如果 `offsetSet` 是 `attach` 的真正别名。我不确定这是故意的还是意外。