Posts

Showing posts with the label recursion

PHP Serialization And Recursion Demystified

Introduction PHP has different in-core callbacks able to help us with daily deployment, debug, improvements. At the same time, PHP is loads of intrinsic " gotcha ", too often hard to understand, hard to explain, or simply hard to manage. One common problem is about debug, caching, or freezing, and the way we would like to debug, cache, or freeze, variables. For freezing, I mean those procedures able to regenerate a stored variable and its status, in order to reuse that variable, to understand what happened in that moment with that variable, or just to speed up expensive tasks already completed. The Problem One of the most common procedures to freeze variables is their serialization, performed in core via a well known serialize function. Please consider this example: $person = new Employee('Mr. Lucky Me'); // ... do some useful task myCompanyFreezer($person); // the myCompanyFreezer function function myCompanyFreezer(Employee $p){ $company = Company::getInstance()...

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