User Contributed Notes 6 notes

Note some inconsistent/surprising behavior in SplObjectStorage to preserve backwards compatibility. You can't properly use foreach with key/value syntax.

<?php$spl = new SplObjectStorage ();$keyForA = new StdClass();$keyForB = new StdClass();$spl[$keyForA] = 'value a';$spl[$keyForB] = 'value b';foreach ($spl as $key => $value){// $key is NOT an object, $value is! // Must use standard array access to get strings.echo $spl[$value] . "\n"; // prints "value a", then "value b"}// it may be clearer to use this form of foreach:foreach ($spl as $key){// $key is an object. // Use standard array access to get values.echo $spl[$key] . "\n"; // prints "value a", then "value b"}?>

I rewrote some scripts and changed object storage with arrays to SplObjectStorage. At some point I needed support of array_rand() but I did not find a function to return a random attached object of an SplObjectStorage object.

#1: A reference to the object itself is stored (not just a hash to compare against the object) and it must be removed before the object is destroyed and the destructor is executed.

#2: SplObjectStorage::rewind() MUST be called to initiate the iterator and before SplObjectStorage::current() will return an object (and I think the only way to retrieve an object?) rather than automatically starting at the first element as I expected it to, like an array for example. This assumption is based on SplObjectStorage::current() returning NULL until SplObjectStorage::rewind() is called once the objects are contained. As a note, always use REWIND before iterating through or fetching objects.