Source of exercise_dump_trace.php
<?php /**/ ?><?php
/*
Exercise the dump object
*/
include('dump.php');
print('<h2>Exercising the trace functionality of the dump object</h2>');
/* set up some data to work with */
$ay = array('Tom'=>5,'Dick'=>9,'Harry'=>10);
/* These traces are buffered */
dump::Trace('Hello world 1');
dump::Trace('Hello world 2','With a label');
dump::Trace('Hello world 3','Never traced',false);
dump::Trace('Hello world 4','Always traced',true);
dump::Trace($ay,'Original array');
/* A class to make objects with which we have instrumented */
class person{
var $name = '';
var $age = 0;
function SimilarAge($Person,$Allowance=2){
// ---- Show how we got here and what the call arguments were ----
dump::Trace('STACK','Comparing ages');
$diff = $this->age - $Person->GetAge();
return abs($diff)<=$Allowance;
}
function GetAge(){
// ---- Only report if a certain condition is met ----
dump::Trace($this,'Conditional on age=9',($this->age==9));
return $this->age;
}
function GetName(){
// ---- Always report ----
dump::Trace($this->name,'Finding name');
return $this->name;
}
function GetInfo(){return $this->Combine($this->GetName(),$this->GetAge());}
function Combine($Name,$Age){return sprintf('<br>%s(%d)',$Name,$Age);}
}
/* Array of three people objects */
$people=array();
foreach($ay as $n=>$a){
$p = new person();
$p->name = $n;
$p->age = $a;
$people[]=$p;
}
// ---- Immediate dump of people array to screen ----
// ---- Note the compact form with properties as table columns ----
print(dump::Value($people));
// ---- This will call GetName() and GetAge() methods ----
// ---- Notice how the trace in GetName() is always called ----
// ---- but the trace in GetAge() is only triggered once ----
foreach($people as $p){print($p->GetInfo());}
/* Exercising stack arguments */
// Infinite loop warning!
// When we come to show Harry we'll show his brother Dick and in turn Harry
// ad-infinitum! How will dump cope?
$people[2]->brother = $people[1]; // Harry has a brother called Dick...
$people[1]->brother = $people[2]; // ...so Dick must have a brother called Harry
// ---- We use SetStackArgumentDetail() to restrict the amount of nesting displayed ----
// ---- Here we will make the same call but with four different levels of detail ----
function Foo($Pple){
dump::Trace('Four stacks with detail 1,2,3,8');
dump::SetStackArgumentDetail(1);
$Pple[1]->SimilarAge($Pple[2],3);
dump::SetStackArgumentDetail(2);
$Pple[1]->SimilarAge($Pple[2]);
dump::SetStackArgumentDetail(3);
$Pple[1]->SimilarAge($Pple[2]);
dump::SetStackArgumentDetail(8);
$Pple[1]->SimilarAge($Pple[2]);
}
Foo($people); // do the deed
print("<br>End of program reached");
/* Save and display results */
dump::Output('',false); // dump to screen but don't reset
print('<hr>'. dump::Output('traceDump.htm',true)); // dump to file
?>