Source for file Var_Dump.php
Documentation is available at Var_Dump.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Frederic Poeydomenge <frederic.poeydomenge@free.fr> |
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Var_Dump::display($vars); |
// | Var_Dump::display($vars,'',VAR_DUMP_DISPLAY_MODE_HTML_TABLE); |
// | $text = Var_Dump::r_display($vars); |
// | $text = Var_Dump::r_display($vars,'',VAR_DUMP_DISPLAY_MODE_TEXT); |
// | $foo=new Var_Dump($confVD); |
// | $foo->setDisplayMode($confDM); |
// | $foo->display($vars); |
// +----------------------------------------------------------------------+
if (!defined('VAR_DUMP_CONSTANTS')) {
define('VAR_DUMP_MAGIC', 'fce75110d95bedd51264ce2a99cfc908');
define('VAR_DUMP_ARRAY_TYPE', 1 );
define('VAR_DUMP_ARRAY_VALUE', 2 );
define('VAR_DUMP_ARRAY_MAGIC', 0 );
define('VAR_DUMP_DISPLAY_MODE_TEXT', 1 );
define('VAR_DUMP_DISPLAY_MODE_HTML_TEXT', 2 );
define('VAR_DUMP_DISPLAY_MODE_HTML_TABLE', 3 );
define('VAR_DUMP_CONSTANTS', true );
// 2003-08-20 Walt Boring <waboring at 3gstech dot com> : feature which
// allows to turn off a few output options for dumping an object
define('VAR_DUMP_NO_CLASS_VARS', 1 );
define('VAR_DUMP_NO_CLASS_METHODS', 2 );
//turns both class vars and class methods off
define('VAR_DUMP_NO_CLASS_INFO', 3 );
* Displays informations about the values of variables on a graphical way
* - If given a simple variable (string, integer, double, ressource), the
* value itself is printed.
* - If given an array, it is explored recursively and values are presented
* in a format that shows keys and elements.
* - If given an object, informations about the object and the class
class Var_Dump extends PEAR
* Selected language for error messages
* Messages in various languages
* @var array $_VarDumpArray An array representing the structure of submitted variables
var $_VarDumpArray = array ();
* @var array $_VarDumpConfGlobal Global parameters for an instance of the class
* 'showNotParsed' => Tell if we show 'not parsed' variables or not
* 'displayMode' => Default display mode : Text or Html
* 'displaySkin' => Default skin name
var $_VarDumpConfGlobal = array ();
* @var array $_VarDumpConfVisual Visual parameters for the rendering
* 'bordercolor' => border color
* 'color1' => cells color for odd rows
* 'color2' => cells color for even rows
* 'bordersize' => border size
* 'cellpadding' => padding of the table
* 'cellspacing' => spacing of the table
* 'fontface' => font face
* 'fontsize' => font size
* 'fontcolor1' => font color for odd rows
* 'fontcolor2' => font color for even rows
var $_VarDumpConfVisual = array ();
* @var string $_VarDumpDisplayFont1Start Text : Starting tag <font...> or empty
* @var string $_VarDumpDisplayFont1End Text : Ending tag </font> or empty
* @var string $_VarDumpDisplayFont2Start Value : Starting tag <font...> or empty
* @var string $_VarDumpDisplayFont2End Value : Ending tag </font> or empty
var $_VarDumpDisplayFont1Start = '';
var $_VarDumpDisplayFont1End = '';
var $_VarDumpDisplayFont2Start = '';
var $_VarDumpDisplayFont2End = '';
* Var_Dump() - Constructor
* If an instance of the class is created, initialize the global parameters for this
* instance : if we show 'not parsed' variables or not, the default display mode,
* the default skin name..
* @param array $conf Global parameters for an instance of the class
* 'showNotParsed' => Tell if we show 'not parsed' variables or not
* 'displayMode' => Default display mode : Text or Html
* 'displaySkin' => Default skin name
function Var_Dump ($conf = array ())
$this->_VarDumpConfGlobal = $conf2;
$this->lang = $conf2['language'];
'TYPE_RESOURCE' => array (
'BOOLEAN_FALSE' => array (
'OBJECT_CLASS_NAME' => array (
'fr' => 'Nom de la classe'
'OBJECT_PARENT' => array (
'OBJECT_CLASS_VARS' => array (
'fr' => 'Variables de la classe'
'OBJECT_CLASS_METHODS' => array (
'OBJECT_OBJECT_VARS' => array (
'fr' => 'Variables d\'instance'
'en' => 'Non-existent variable',
'fr' => 'Variable inexistente'
* display() - Displays informations about a variable
* This method displays informations about the values of a variable on a
* graphical way, arrays and objects being explored recursively
* @param $variable A variable to explore
* @param string $except If not empty, name of the key not to
* explore, to avoid parsing references to itself
function display ($variable = '', $except = '', $displayMode = false , $options=null )
if (!isset ($this) or get_class($this) != 'var_dump') {
if ($displayMode !== false ) {
$obj->_VarDumpConfGlobal ['displayMode'] = $displayMode;
$obj->setSkin ($displayMode);
echo $obj->r_display ($variable, $except, $displayMode, $options);
* r_display() - Returns informations about a variable, ready to be printed
* This method returns informations about the values of a variable, ready
* to be printed on a graphical way, arrays and objects being explored
* @param $variable A variable to explore
* @param string $except If not empty, name of the key not to
* explore, to avoid parsing references to itself
function r_display ($variable = '', $except = '', $displayMode = false , $options=null )
if (!isset ($this) or get_class($this) != 'var_dump') {
if ($displayMode !== false ) {
$obj->_VarDumpConfGlobal ['displayMode'] = $displayMode;
$obj->setSkin ($displayMode);
$obj->_VarDumpConfGlobal ['displayOptions'] = $options;
$obj->_populate ($variable, $except);
$obj->_generateFontTags ();
switch($obj->_VarDumpConfGlobal ['displayMode']) {
return $obj->_formatTableTEXT ($obj->_VarDumpArray [0 ],false );
return $obj->_formatTableTEXT ($obj->_VarDumpArray [0 ],true );
return $obj->_formatTableHTML ($obj->_VarDumpArray );
* _populate() - Fills the informations concerning a single variable
* This method fills the local array $this->_VarDumpArray with the
* informations concerning a single variable
* When parsing $GLOBALS variable, avoid parsing recursively the
* @param $variable The variable to explore
* @param string $except If not empty, name of the key not to
* explore, to avoid parsing references to itself
function _populate ($variable, $except = '')
$this->_VarDumpArray=array ();
$w_except = ' '. trim($except);
if (//$variable == $GLOBALS does not work
isset ($variable['_GET']) and
isset ($variable['_POST']) and
isset ($variable['_COOKIE']) and
isset ($variable['_SERVER']) and
$this->_VarDumpArray[] = $this->_parseArray ($GLOBALS, 'GLOBALS'. $w_except);
$this->_VarDumpArray[] = $this->_parseArray ($variable, $except);
$this->_VarDumpArray[] = $this->_parseObject ($variable);
$this->_VarDumpArray[] = $this->_parseVariable ($variable);
* _parseVariable() - Parse (recursively) a variable
* This method parse a variable, either returning informations about
* this variable, or in the case of an object or array, returning
* recursive informations about this variable.
* @param $variable A variable to explore
function _parseVariable ($variable)
return $this->_parseObject ($variable);
return $this->_parseArray ($variable);
$type = $this->messages['TYPE_LONG'][$lang];
} elseif (is_int($variable)) {
$type = $this->messages['TYPE_INT'][$lang];
$type = $this->messages['TYPE_INTEGER'][$lang];
$type = $this->messages['TYPE_DOUBLE'][$lang];
$type = $this->messages['TYPE_FLOAT'][$lang];
$type = $this->messages['TYPE_REAL'][$lang];
$type = $this->messages['TYPE_STRING'][$lang]. '['. strlen($variable). ']';
$type = $this->messages['TYPE_BOOLEAN'][$lang];
$variable = $this->messages['BOOLEAN_TRUE'][$lang];
$variable = $this->messages['BOOLEAN_FALSE'][$lang];
$type = $this->messages['TYPE_NUMERIC'][$lang];
$type = $this->messages['TYPE_RESOURCE'][$lang]. '['. get_resource_type($variable). ']';
$type = $this->messages['TYPE_SCALAR'][$lang];
$type = $this->messages['TYPE_NULL'][$lang];
$type = $this->messages['TYPE_UNKNOWN'][$lang]. '['. gettype($variable). ']';
* _parseArray() - Parse recursively an array
* This method returns recursive informations on an array :
* structure, keys and values
* @param array $array An array to explore
* @param string $except If not empty, name of the key not to
* explore, to avoid parsing references to itself
function _parseArray ($array, $except = '')
$except_array = explode(' ',$except);
return $this->_parseVariable ($array);
foreach($array as $key => $value) {
if (! (in_array($key, $except_array, TRUE ))) {
$localArray[$key] = $this->_parseArray ($value, $except);
$localArray[$key] = array (
* _parseObject() - Returns informations on an object
* This method returns informations on an object and its class :
* default class variables and methods, current object state
* @param object $object An object to explore
function _parseObject ($object)
return $this->_parseVariable ($object);
$this->messages['OBJECT_CLASS_NAME'][$lang] => $this->_parseVariable ($className),
$this->messages['OBJECT_PARENT'][$lang] => $this->_parseVariable (get_parent_class($object)),
$this->messages['OBJECT_CLASS_VARS'][$lang] => $this->_parseArray (get_class_vars($className)),
$this->messages['OBJECT_CLASS_METHODS'][$lang] => $this->_parseArray (get_class_methods($className)),
$this->messages['OBJECT_OBJECT_VARS'][$lang] => $this->_parseArray (get_object_vars($object))
unset ($arr[$this->messages['OBJECT_CLASS_VARS'][$lang]]);
unset ($arr[$this->messages['OBJECT_CLASS_METHODS'][$lang]]);
unset ($arr[$this->messages['OBJECT_CLASS_VARS'][$lang]]);
unset ($arr[$this->messages['OBJECT_CLASS_METHODS'][$lang]]);
// {{{ _isSingleVariable()
* _isSingleVariable() - Tells if a variable is a single variable
* This method tells if a variable is a single variable (long,
* string, double...) or a more complex one (array, object...)
* @param $variable The variable to check
* @return True if it's a single variable
* False if it's a more complex variable
function _isSingleVariable ($variable)
// {{{ _generateFontTags()
* _generateFontTags() - Generates the font tags <font...>
* This method generates the font tags <font...> with the
* font size, color and face values choosen. If none of the
* font parameters was modified, use the default font.
function _generateFontTags ()
if (!empty ($this->_VarDumpConfVisual['fontface'])) {
$font .= ' face="'. $this->_VarDumpConfVisual['fontface']. '"';
if (!empty ($this->_VarDumpConfVisual['fontsize'])) {
$font .= ' size="'. $this->_VarDumpConfVisual['fontsize']. '"';
for ($i = 1 ; $i <= 2 ; $i++ ) {
if (!empty ($this->_VarDumpConfVisual['fontcolor'. $i])) {
$fontTag .= ' color="'. $this->_VarDumpConfVisual['fontcolor'. $i]. '"';
$this->{'_VarDumpDisplayFont'. $i. 'Start'} = '<font'. $fontTag. '>';
$this->{'_VarDumpDisplayFont'. $i. 'End'} = '</font>';
$this->{'_VarDumpDisplayFont'. $i. 'Start'} = '';
$this->{'_VarDumpDisplayFont'. $i. 'End'} = '';
// {{{ _formatTableTEXT()
* _formatTableTEXT() - Returns informations in text format
* This method returns all the informations collected on
* the submitted variables, in a text format
* @param array $array The _VarDumpArray structure to display
* @param boolean $html Tell whether the output have to be html formated or not
function _formatTableTEXT ($array,$html)
$tmp = $this->_formatTableTEXTArray ($array, $html);
$tmp = ereg_replace("(^([\n])*)|(^(<br>)*)|(([\n])*$)|((<br>)*$)", '', $tmp);
$this->_VarDumpDisplayFont1Start.
$this->_VarDumpDisplayFont1End.
// {{{ _formatTableTEXTArray()
* _formatTableTEXTArray() - Returns informations in text format
* This recursive method returns all the informations collected on
* the submitted variables, in a text format
* @param array $array The _VarDumpArray structure to display
* @param boolean $html Tell whether the output have to be html formated or not
* @param array $complete Contains the width of each column
* @param int $level Level of recursion
function _formatTableTEXTArray ($array, $html = false , $complete = array (), $level = 0 )
$table .= $this->_formatTableTEXTSeparator ($complete, ' ');
if (is_array($array) and !$this->_isSingleVariable ($array)) {
foreach($array AS $key=> $val) {
if (strlen($key) > $largeurCle) {
if ($this->_isSingleVariable ($val)) {
$str = $this->messages ['TYPE_ARRAY'][$lang]. '('. count($val). ')';
if ($len > $largeurType) {
$compteur = count($array);
foreach($array AS $key=> $val) {
$table .= $this->_formatTableTEXTSeparator ($complete, '+');
} elseif ($k == $compteur-1 ) {
$table .= $this->_formatTableTEXTSeparator ($complete, '+');
$table .= $this->_formatTableTEXTSeparator ($complete, '|');
if ($this->_isSingleVariable ($val)) {
sprintf('%-'. $largeurCle. 's', $key).
$this->_VarDumpDisplayFont2Start.
$this->_VarDumpDisplayFont2End.
sprintf('%-'. $largeurCle. 's', $key).
sprintf('%-'. $largeurType. 's', $this->messages ['TYPE_ARRAY'][$lang]. '('. count($val). ')');
$table .= $this->_formatTableTEXTArray ($val, $html, $complete, $level + 1 );
$this->_VarDumpDisplayFont2Start.
$this->_VarDumpDisplayFont2End.
$table .= $this->_formatTableTEXTSeparator ($complete, ' ');
// {{{ _formatTableTEXTSeparator()
* _formatTableTEXTSeparator() - Returns the beginning of a line
* @param array $complete Contains the width of each column
* @param char $displayLast Last character to display on the line
function _formatTableTEXTSeparator ($complete, $displayLast = '')
$count = count($complete);
foreach($complete AS $larg) {
// {{{ _formatTableHTML()
* _formatTableHTML() - Returns informations in HTML format
* This method returns all the informations collected on
* the submitted variables, in a graphical format
* @param array $array The _VarDumpArray structure to display
* @param int $level Level of recursion
function _formatTableHTML ($array, $level = 0 )
$table_footer .= '</table>';
if ($this->_VarDumpConfVisual ['bordersize'] > 0 ) {
$table_header .= '<table border=0 cellpadding='. $this->_VarDumpConfVisual ['bordersize']. ' cellspacing=0 bgcolor='. $this->_VarDumpConfVisual ['bordercolor']. '><tr><td>';
$table_footer .= '</td></tr></table>';
$table_header .= '<table border=0 cellpadding='. $this->_VarDumpConfVisual ['cellpadding']. ' cellspacing='. $this->_VarDumpConfVisual ['cellspacing']. '>';
if ($this->_isSingleVariable ($array)) {
$table .= $this->_VarDumpDisplayFont1Start;
$this->_VarDumpDisplayFont2Start.
$this->_VarDumpDisplayFont2End. ')';
$this->_VarDumpDisplayFont2Start.
$this->_VarDumpDisplayFont2End. ')';
$table .= $this->_VarDumpDisplayFont1End;
if ($level==0 and count($array)==1 and !$this->_isSingleVariable ($array[0 ]) and is_array($array[0 ])) {
foreach($array as $key => $value) {
if ($this->_isSingleVariable ($value)) {
$countValue = ' ('. count($value). ')';
if (!empty ($this->_VarDumpConfVisual ['color1'])) {
$bg = ' bgcolor='. $this->_VarDumpConfVisual ['color1'];
if (!empty ($this->_VarDumpConfVisual ['color2'])) {
$bg = ' bgcolor='. $this->_VarDumpConfVisual ['color2'];
$table .= '<tr'. $bg. ' valign=top>';
if ($level != 0 or count($array) != 1 ) {
$table .= '<td>'. $this->_VarDumpDisplayFont1Start. $key. $countValue. $this->_VarDumpDisplayFont1End. '</td>';
$table .= '<td>'. $this->_formatTableHTML ($value, $level+1 ). '</td>';
$table .= $this->_formatTableHTML ($value, $level+1 );
* setDisplayMode() - Set visual parameters
* This method set visual parameters for the rendering : border color
* and size, cells colors, padding and spacing of the table, font face,
* @param array $conf Display configuration
function setDisplayMode ($conf = array ())
'bordercolor' => '#444444',
$this->_VarDumpConfVisual = $conf2;
* setSkin() - Set visual parameters using a skin model
* This method set visual parameters using a skin model
* @param int $mode Default display mode : Text or Html
* @param string $skin Name of the skin
* for the moment : default, green, red or blue
function setSkin ($mode = VAR_DUMP_DISPLAY_MODE_HTML_TEXT , $skin = 'default')
$this->setDisplayMode (array ('fontcolor2' => '#55CC5F'));
$this->setDisplayMode (array ('fontcolor2' => '#CC555F'));
$this->setDisplayMode (array ('fontcolor2' => '#5F55CC'));
'bordercolor' => '#2BB036',
'fontcolor1' => '#FFFFFF',
'fontcolor2' => '#FF00CC'
'bordercolor' => '#B02B36',
'fontcolor1' => '#FFFFFF',
'fontcolor2' => '#FFCC00'
'bordercolor' => '#362BB0',
'fontcolor1' => '#FFFFFF',
'fontcolor2' => '#CCFF00'
Documentation generated on Mon, 11 Mar 2019 10:16:33 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|