note 113620 added to function.print-r

From: Date: Wed, 06 Nov 2013 16:06:38 +0000
Subject: note 113620 added to function.print-r
Groups: php.notes 
Request: Send a blank email to php-notes+get-197445@lists.php.net to get a copy of this message
Here is a print_r() clone but support max level limit. When we want to print a big object, this will help us get a clean dumping data. <?php /** * Recrusive print variables and limit by level. * * @param mixed $data The variable you want to dump. * @param int $level The level number to limit recrusive loop. * * @return string Dumped data. * * @author Simon Asika (asika32764[at]gmail com) * @date 2013-11-06 */ function print_r_level($data, $level = 5) { static $innerLevel = 1; static $tabLevel = 1; static $cache = array(); $self = __FUNCTION__; $type = gettype($data); $tabs = str_repeat(' ', $tabLevel); $quoteTabes = str_repeat(' ', $tabLevel - 1); $recrusiveType = array('object', 'array'); // Recrusive if (in_array($type, $recrusiveType)) { // If type is object, try to get properties by Reflection. if ($type == 'object') { if (in_array($data, $cache)) { return "\n{$quoteTabes}*RECURSION*\n"; } // Cache the data $cache[] = $data; $output = get_class($data) . ' ' . ucfirst($type); $ref = new \ReflectionObject($data); $properties = $ref->getProperties(); $elements = array(); foreach ($properties as $property) { $property->setAccessible(true); $pType = $property->getName(); if ($property->isProtected()) { $pType .= ":protected"; } elseif ($property->isPrivate()) { $pType .= ":" . $property->class . ":private"; } if ($property->isStatic()) { $pType .= ":static"; } $elements[$pType] = $property->getValue($data); } } // If type is array, just retun it's value. elseif ($type == 'array') { $output = ucfirst($type); $elements = $data; } // Start dumping datas if ($level == 0 || $innerLevel < $level) { // Start recrusive print $output .= "\n{$quoteTabes}("; foreach ($elements as $key => $element) { $output .= "\n{$tabs}[{$key}] => "; // Increment level $tabLevel = $tabLevel + 2; $innerLevel++; $output .= in_array(gettype($element), $recrusiveType) ? $self($element, $level) : $element; // Decrement level $tabLevel = $tabLevel - 2; $innerLevel--; } $output .= "\n{$quoteTabes})\n"; } else { $output .= "\n{$quoteTabes}*MAX LEVEL*\n"; } } // Clean cache if($innerLevel == 1) { $cache = array(); } return $output; }// End function // TEST ------------------------------------ class testClass { protected $a = 'aaa'; private $b = 'bbb'; public $c = array(1, 2, ['a', 'b', 'c'], 4); static public $d = 'ddd'; static protected $e = 'eee'; } $test = new testClass; $test->testClass = $test; echo '<pre>' . print_r_level($test, 3) . '</pre>'; ?> will output ------------------------------------------------------------- testClass Object ( [a:protected] => aaa [b:testClass:private] => bbb [c] => Array ( [0] => 1 [1] => 2 [2] => Array *MAX LEVEL* [3] => 4 ) [d:static] => ddd [e:protected:static] => eee [testClass] => *RECURSION* ) ---- Server IP: 72.52.91.14 Probable Submitter: 72.52.91.14 ---- Manual Page -- http://php.net/manual/en/function.print-r.php Edit -- https://master.php.net/note/edit/113620 Del: integrated -- https://master.php.net/note/delete/113620/integrated Del: useless -- https://master.php.net/note/delete/113620/useless Del: bad code -- https://master.php.net/note/delete/113620/bad+code Del: spam -- https://master.php.net/note/delete/113620/spam Del: non-english -- https://master.php.net/note/delete/113620/non-english Del: in docs -- https://master.php.net/note/delete/113620/in+docs Del: other reasons-- https://master.php.net/note/delete/113620 Reject -- https://master.php.net/note/reject/113620 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#197445) next »