Access SplObjectStorage data through Reflection

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

+4
source share
1 answer

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

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

A tangentially related problem shows the following snippet:

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

In the above code, you will be told that the class does not have a timezone property, even if you can access the timezone property on DateTime objects. The reason is that this property is not declared, it is provided only through the internal get_properties handler. Once again, this property, which is not intended for direct access, exists only for: a) providing meaningful debug output and b) determining what should be serialized when the object is serialized.

In short: Reflection on the โ€œpropertiesโ€ of inner classes usually does not work, simply because these properties often do not actually exist.

+5
source

All Articles