note 126098 added to class.splobjectstorage
| From: | jan at odvarko dot cz | Date: | Thu, 20 May 2021 18:32:55 +0000 |
| Subject: | note 126098 added to class.splobjectstorage | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-216412@lists.php.net to get a copy of this message | ||
If you assign an array() to an object in SplObjectStorage and then try to modify its individual
elements, you'll probably find it doesn't work.
Instead, you can use ArrayObject(), which will emulate array behaviour.
<?php
$storage = new SplObjectStorage();
$obj1 = new StdClass();
$obj2 = new StdClass();
$storage[$obj1] = array();
$storage[$obj2] = new ArrayObject();
$storage[$obj1]['person'] = 'Jana'; // Won't work (PHP Notice: Indirect
modification of overloaded element of SplObjectStorage has no effect)
$storage[$obj2]['person'] = 'Jana'; // Works
var_dump($storage[$obj1]['person']); // NULL (PHP Notice: Undefined index: person)
var_dump($storage[$obj2]['person']); // string(4) "Jana"
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.242 (proxied: 212.11.118.132)
----
Manual Page -- https://php.net/manual/en/class.splobjectstorage.php
Edit -- https://main.php.net/note/edit/126098
Del: integrated -- https://main.php.net/note/delete/126098/integrated
Del: useless -- https://main.php.net/note/delete/126098/useless
Del: bad code -- https://main.php.net/note/delete/126098/bad+code
Del: spam -- https://main.php.net/note/delete/126098/spam
Del: non-english -- https://main.php.net/note/delete/126098/non-english
Del: in docs -- https://main.php.net/note/delete/126098/in+docs
Del: other reasons-- https://main.php.net/note/delete/126098
Reject -- https://main.php.net/note/reject/126098
Search -- https://main.php.net/manage/user-notes.php