PHP 8.5.0 RC 1 available for testing

Voting

: min(six, five)?
(Example: nine)

The Note You're Voting On

jan at odvarko dot cz
4 years ago
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"

?>

<< Back to user notes page

To Top