The cu-debug-functions.php contains two functions which can inspect variables in php codes.
- data_console( mixed $var, String $label = null );
- To display the result in the browser console.
- data_echo( mixed $var, String $label = null )
- To echo the result on the web page.
The variable to be inspected can be of type object, array or plain variable.
Usage:
- Include_once the “cu-debug-functions.php” in the php program. For WordPress, use the functions.php to include this file.
- At the appropriate location of the program, add the above command.
<?php
function data_console($data, $label=null) {
/**^ Output $data to console for inspection.
* $data can be any variable types.
*
* For wordpress, 'include' this file in functions.php of the theme.
*
* @param mixed $data The variable to be inspected.
* @param string $label The name of the variable. Optional. *
*
* */
echo("<script> console.log('Var Name: ". $label. "'); </script>");
/*Output the data type */
$datatype = gettype($data);
$result = "Data type: ". $datatype;
echo("<script> console.log('". $result. "'); </script>");
if(is_object($data)){
/*Output class name */
if(class_exists(get_class($data), true)) {
$result = "Class Name: ". get_class($data);
echo("<script> console.log('". $result. "'); </script>");
}
$reflection = new ReflectionClass(get_class($data));
/* Output the methods */
$methodArr = $reflection->getMethods();
$i = 0;
foreach ($methodArr as $key=>$value) {
$i ++;
// avoid console.log breaking, escape ' char by replacing it with ~.
$esc_value = str_replace("'", "~", $value);
$output = json_encode($esc_value);
echo("<script> console.log('------------------'); console.log('Method ". $i. ": '); console.log('". $output. "'); </script>");
}
echo("<script> console.log(' '); </script>");
/* Output properties */
$vars = $reflection->getProperties();
$i = 0;
foreach($vars as $key => $value){
$i ++;
$esc_value = str_replace("'", "~", $value);
$output = json_encode($esc_value);
echo("<script> console.log('------------------'); console.log('Property ". $i. ": '); console.log('". $output. "'); </script>");
}
/*Output var_dump of object */
ob_start(); //start the output buffer.
print_r($data);
$result = ob_get_clean();
$search = array("'", "\n", "\r");
$replace = array("~", "=?=", "=?=");
$output = str_replace($search, $replace, $result, $count);
// $output = str_replace("'", "~", $result);
// $output = preg_replace("/[\n\r]/", "", $output);
echo("<script> console.log('------------------'); console.log('". $label. " var_dump[replaced ". $count. "]:'); </script>");
$propArr = explode("=?=", $output);
foreach($propArr as $prop) {
echo("<script> console.log('". $prop. "'); </script>");
}
//echo("<script> console.log('". $output. "'); </script>");
} else {
// if $data is not an object, do the followings.
$value = str_replace("'", "~", $data);
$output = json_encode($value);
echo("<script> console.log('". $output. "'); console.log(''); </script>");
}
} // end data_console()
function data_echo($data, $label=null) {
/**^ Echo $data to page for inspection.
* $data can be any variable types.
*
* For wordpress, 'include' this file in functions.php of the theme.
*
* @param mixed $data The variable to be inspected.
* @param string $label The name of the variable. Optional. *
*
* */
echo "<b>Var Name: $label<br>";
/*Output the data type */
$datatype = gettype($data);
echo "Data Type: $datatype </b><br>";
if(is_object($data)){
/*Output class name */
if(class_exists(get_class($data), true)) {
$result = "Class Name: ". get_class($data);
echo "<b>$result </b><br><br>";
}
/*Output the object */
ob_start(); //start the output buffer.
var_export($data);
$result = ob_get_clean();
$search = array("\n", "\r", "),");
$replace = array("<br>", "<br>", "),<br>");
$output = str_replace($search, $replace, $result, $count);
echo "<b>======================================</b><br>";
echo "<b>Object Properties: </b><br><br>";
echo "$output <br><br>";
$reflection = new ReflectionClass(get_class($data));
/* Output the methods */
echo "<b style='text-decoration:underline;'>Class Methods: </b><br><br>";
$methodArr = $reflection->getMethods();
$i = 0;
foreach ($methodArr as $key=>$value) {
$i ++;
$search = array(" *", "*/", "`", "] {");
$replace = array("<br>*", "*/<br>", "'", "]<br>{");
$output = str_replace($search, $replace, $value);
echo "<b>Method $i : </b><br>";
echo "$output <br><br>";
}
/* Output properties */
echo "<b style='text-decoration:underline;'>Class Properties: </b><br><br>";
$vars = $reflection->getProperties();
$i = 0;
foreach($vars as $key => $value){
$i ++;
$output = str_replace('\n', '', $value);
echo "<b>Property $i : </b><br>";
echo "$output <br><br>";
}
} else {
// if $data is not an object, do the followings.
ob_start(); //start the output buffer.
var_export($data);
$result = ob_get_clean();
$output = str_replace(')),', ')),<br>', $result);
echo "<br>$output <br>";
} // end if-else
echo "<br><b>=== Finished ===</b><br><br>";
} // end data_echo()
?>
