雖然 `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` 的別名。我不確定這是否是故意的。