class - How do I get a single level of object details from php? -
when retrieve stacktrace in php using debug_backtrace, can have include object called from. beingness able see both public , private properties of object extremely useful me, object big usable. due of variables in containing other objects, printing out not feasible - tried writing output of debug_print_backtrace file, , reached 23g before broke , stopped outputting.
what need, output limit going 1 level deep - is, output properties of object, if 1 of object, instead of farther listing out 1 need class name it.
i know can utilize get_object_vars public properties of object, how private ones? print_r, var_export, var_dump, etc. useless, because can't stop them recursing.
you can cast objects arrays, iterate on array. private , protected properties available too, prefixed class name enclosed null-bytes. can test behaviour so:
test:
class test { private $parent; public function __construct($parent) { $this->parent = $parent; } } $test = new test(new test(new test(null))); var_dump((array)$test);
output:
array(1) { ["�test�parent"]=> object(test)#2 (1) { ["parent":"test":private]=> object(test)#3 (1) { ["parent":"test":private]=> null } } }
now have array can simple foreach
, stripping prefixes , differentiating between objects , other variables:
foreach((array)$test $name => $property) { if (ord($name[0]) === 0) { $name = substr($name, strrpos($name, "\x0")+1); } echo $name, ":"; if (is_object($property)) { echo "(object)", get_class($property); } else { echo var_export($property, true); } echo "\n"; }
i'll leave pretty-printing you.
php class object stack-trace
No comments:
Post a Comment