note 128662 added to splobjectstorage.offsetset

From: Date: Wed, 05 Jul 2023 15:48:08 +0000
Subject: note 128662 added to splobjectstorage.offsetset
Groups: php.notes 
Request: Send a blank email to php-notes+get-220623@lists.php.net to get a copy of this message
Although offsetSet is supposed to be an alias to attach, real-world results do not indicate that. When extending the SplObjectStorage class, it's expected that a modification to attach would also affect offsetSet. However, it seems they need to both be extended. Consider the results below... <?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'); ?> This prints: ``` offsetSet called attach called ``` While we would expect... ``` offsetSet called attach called attach called ``` ... if offsetSet was a true alias of attach. I'm not sure if this is intentional or not. ---- Server IP: 45.112.84.4 Probable Submitter: 45.91.159.168 (proxied: 161.69.123.10) ---- Manual Page -- https://php.net/manual/en/splobjectstorage.offsetset.php Edit -- https://main.php.net/note/edit/128662 Del: integrated -- https://main.php.net/note/delete/128662/integrated Del: useless -- https://main.php.net/note/delete/128662/useless Del: bad code -- https://main.php.net/note/delete/128662/bad+code Del: spam -- https://main.php.net/note/delete/128662/spam Del: non-english -- https://main.php.net/note/delete/128662/non-english Del: in docs -- https://main.php.net/note/delete/128662/in+docs Del: other reasons-- https://main.php.net/note/delete/128662 Reject -- https://main.php.net/note/reject/128662 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#220623) next »