Question

Is it possible to access the data of SplObjectStorage using Reflection or some other method? When I use print_r on it, I can see there is a private property $storage with an array containing all the data, but I cannot access it using Reflection in any way. Is there some other possible solution to get the data without iterating over the collection with foreach?

Was it helpful?

Solution

It is not possible to access the $storage property through Reflection because it doesn't exist.

What you see when you call print_r (or var_dump) on the class is debug information. This information is provided through the internal get_debug_info handler of the class. This handler allows internal classes to display meaningful debugging information without defining actual properties.

A tangentially related issue shows with the following snippet:

$r = new ReflectionClass('DateTime');
var_dump($r->hasProperty("timezone"));

The above code will tell you that the class has no timezone property, even though you can access the timezone property on DateTime objects. The reason is that this property is not declared, it is only provided through the internal get_properties handler. Once again, this is a property that is not designed to be accessed directly, it only exists to a) provide meaningful debugging output and b) specify what should be serialized when the object is serialized.

In summary: Reflecting on "properties" of internal classes will usually not work out, simply because those properties often do not actually exist.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top