var_dump
This function displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure.
Since var_dump explores objects recursively and __debuginfo can be used to change the output of var_dump.
Then, is it possible to detect the top most object where the var_dump recursion started and return output based on certain condition?
For Example:
public function __debuginfo() {
$debug = array(
'name' => 'Ucscode',
'profession' => 'programming'
);
if( $this != 'The object passed by user to var_dump' ) return $debug;
return array('type' => "The object");
}
Update:
I tried something like this and it worked
public function __debuginfo() {
global $customDebugLevel; // a counter;
if( is_null($customDebugLevel) ) $customDebugLevel = -1; // 1st init
$customDebugLevel++; // increase per var_dump call
$debug = array(
'name' => 'Ucscode',
'profession' => 'programming',
'child' => $this->child
);
if( empty($customDebugLevel) ) return $debug;
return array('type' => "The object");
}
The problem however is that if var_dump is called twice on an instance of same class, it will not reset the $customDebugLevel therefore, only the first var_dump call will produce the correct result
I don't think there's any built-in way. But you can set a variable to the top-level object before calling
var_dump(), and compare with that.You can encapsulate this in a function: