note 106202 deleted from function.print-r by danbrown

From: Date: Tue, 18 Oct 2011 14:45:26 +0000
Subject: note 106202 deleted from function.print-r by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-183961@lists.php.net to get a copy of this message
Note Submitter: darkdevine at hotmail dot de ---- None of the custom print_r functions here and in the var_dump notes worked for me. The biggest problem regarding such functions is the ability to find references to prevent recursion. Imagine the following code: <?php $root = new XmlRoot( 'myRoot' ); $node = new XmlNode( 'myNode' ); $root->AddChildren( $node ); // AddChildren will add $node to $root->_Children and will set $node->_Root to $root ?> If we iterate $node or $root using a custom dump function that uses foreach() to jump over the values, the properties would permanently reference themself and print again and again, which is called recursion Now here's my solution for this: http://pastebin.com/B8JRA5XE This function prints the passed argument in a very clean and nice format using colors and white-spaces. I prevent recursion like this: Upon calling the function, the passed argument is serialized. The MD5 hash of the serialized string is the key we use to detect, if the same object/array has already passed. We don't really save the values, we just save the hash (I prefer isset() over in_array, that's why I'm only using the keys there) Then the function will check, if the MD5 string already exists, if yes, the type that falls into the switch() is changed to "recursion" and prints a string later on. I use many static variables for the sake of usability, the function accepts multiple parameters like var_dump does and prints every single one of them in a nice format (or rather, it gives the string to print to you, which has more use than just echo'ing it imho) Feel free to use it

« previous php.notes (#183961) next »