Sunday, September 6, 2009

PHP How to var_export a debug_backtrace

Just a quick post, and I am not sure if this is documented, or if this works with every PHP version, but apparently a truly common problem with var_export is the logical inability to manage recursions.

Well, one of the most important function in PHP is the debug_backtrace, which contain recursion, and one thing you would probably like to know is that this trick allowed me to avoid recursion problems:

$backtrace = unserialize(
serialize(debug_backtrace())
);

After this, it is possible to use json_encode, var_export, or whatever other serializer.
The reason is that serialization discover and remove some implicit recursion, so that unserialize will return a cleaner object.
This technique has been used in Formaldehyde, but manual recursions are not supported yet (for example via formaldehyde_log). recursions, for a debug purpose, are in any case redundant. All we need to know is what is there, rather than where.
Regards

No comments:

Post a Comment